]> gcc.gnu.org Git - gcc.git/blame - gcc/c-common.c
cp-tree.h: Declare flag_use_repository.
[gcc.git] / gcc / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
ad83f537 2 Copyright (C) 1992, 93-98, 1999 Free Software Foundation, Inc.
b30f223b
RS
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
940d9d63
RK
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
b30f223b
RS
20
21#include "config.h"
670ee920 22#include "system.h"
b30f223b
RS
23#include "tree.h"
24#include "c-lex.h"
25#include "c-tree.h"
26#include "flags.h"
1bb8e5b1 27#include "obstack.h"
5f6da302 28#include "toplev.h"
d6f4ec51 29#include "output.h"
e2af664c 30#include "c-pragma.h"
3932261a 31#include "rtl.h"
ccd043a9 32
c8724862
DB
33#if USE_CPPLIB
34#include "cpplib.h"
35cpp_reader parse_in;
36cpp_options parse_options;
3773a46b 37enum cpp_token cpp_token;
c8724862
DB
38#endif
39
fef610be
JW
40#ifndef WCHAR_TYPE_SIZE
41#ifdef INT_TYPE_SIZE
42#define WCHAR_TYPE_SIZE INT_TYPE_SIZE
43#else
44#define WCHAR_TYPE_SIZE BITS_PER_WORD
45#endif
46#endif
47
7f4edbcb
BS
48/* The following symbols are subsumed in the c_global_trees array, and
49 listed here individually for documentation purposes.
50
51 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
52
53 tree short_integer_type_node;
54 tree long_integer_type_node;
55 tree long_long_integer_type_node;
56
57 tree short_unsigned_type_node;
58 tree long_unsigned_type_node;
59 tree long_long_unsigned_type_node;
60
61 tree boolean_type_node;
62 tree boolean_false_node;
63 tree boolean_true_node;
64
65 tree ptrdiff_type_node;
66
67 tree unsigned_char_type_node;
68 tree signed_char_type_node;
69 tree wchar_type_node;
70 tree signed_wchar_type_node;
71 tree unsigned_wchar_type_node;
72
73 tree float_type_node;
74 tree double_type_node;
75 tree long_double_type_node;
76
77 tree complex_integer_type_node;
78 tree complex_float_type_node;
79 tree complex_double_type_node;
80 tree complex_long_double_type_node;
81
82 tree intQI_type_node;
83 tree intHI_type_node;
84 tree intSI_type_node;
85 tree intDI_type_node;
86 tree intTI_type_node;
87
88 tree unsigned_intQI_type_node;
89 tree unsigned_intHI_type_node;
90 tree unsigned_intSI_type_node;
91 tree unsigned_intDI_type_node;
92 tree unsigned_intTI_type_node;
93
94 tree widest_integer_literal_type_node;
95 tree widest_unsigned_literal_type_node;
96
97 Nodes for types `void *' and `const void *'.
98
99 tree ptr_type_node, const_ptr_type_node;
100
101 Nodes for types `char *' and `const char *'.
102
103 tree string_type_node, const_string_type_node;
104
105 Type `char[SOMENUMBER]'.
106 Used when an array of char is needed and the size is irrelevant.
107
108 tree char_array_type_node;
109
110 Type `int[SOMENUMBER]' or something like it.
111 Used when an array of int needed and the size is irrelevant.
112
113 tree int_array_type_node;
114
115 Type `wchar_t[SOMENUMBER]' or something like it.
116 Used when a wide string literal is created.
117
118 tree wchar_array_type_node;
119
120 Type `int ()' -- used for implicit declaration of functions.
121
122 tree default_function_type;
123
124 Function types `int (int)', etc.
125
126 tree int_ftype_int;
127 tree void_ftype;
128 tree void_ftype_ptr;
129 tree int_ftype_int;
130 tree ptr_ftype_sizetype;
131
132 A VOID_TYPE node, packaged in a TREE_LIST.
133
134 tree void_list_node;
135
136*/
137
138tree c_global_trees[CTI_MAX];
0b73773c 139
e78a3b42
RK
140/* Nonzero means the expression being parsed will never be evaluated.
141 This is a count, since unevaluated expressions can nest. */
142int skip_evaluation;
143
2786cbad 144enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
7d384cc0 145 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
bbb1ae01 146 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
adfaf194 147 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
53065596 148
bb72a084
PE
149enum format_type { printf_format_type, scanf_format_type,
150 strftime_format_type };
151
dff01034
KG
152static void declare_hidden_char_array PROTO((const char *, const char *));
153static void add_attribute PROTO((enum attrs, const char *,
53065596
RK
154 int, int, int));
155static void init_attributes PROTO((void));
bb72a084
PE
156static void record_function_format PROTO((tree, tree, enum format_type,
157 int, int));
0161e8da 158static void record_international_format PROTO((tree, tree, int));
3932261a 159static tree c_find_base_decl PROTO((tree));
adfaf194 160static int default_valid_lang_attribute PROTO ((tree, tree, tree, tree));
4724b3de 161
0a7ed33c
BS
162/* Keep a stack of if statements. We record the number of compound
163 statements seen up to the if keyword, as well as the line number
164 and file of the if. If a potentially ambiguous else is seen, that
165 fact is recorded; the warning is issued when we can be sure that
166 the enclosing if statement does not have an else branch. */
167typedef struct
168{
169 int compstmt_count;
170 int line;
dff01034 171 const char *file;
0a7ed33c
BS
172 int needs_warning;
173} if_elt;
ab87f8c8 174static void tfaff PROTO((void));
0a7ed33c
BS
175
176static if_elt *if_stack;
6d819282
MK
177
178/* Amount of space in the if statement stack. */
179static int if_stack_space = 0;
180
181/* Stack pointer. */
182static int if_stack_pointer = 0;
183
0a7ed33c
BS
184/* Generate RTL for the start of an if-then, and record the start of it
185 for ambiguous else detection. */
186
6d819282
MK
187void
188c_expand_start_cond (cond, exitflag, compstmt_count)
189 tree cond;
190 int exitflag;
191 int compstmt_count;
192{
193 /* Make sure there is enough space on the stack. */
194 if (if_stack_space == 0)
195 {
196 if_stack_space = 10;
0a7ed33c 197 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
6d819282
MK
198 }
199 else if (if_stack_space == if_stack_pointer)
200 {
201 if_stack_space += 10;
0a7ed33c 202 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
6d819282 203 }
0a7ed33c 204
6d819282 205 /* Record this if statement. */
0a7ed33c
BS
206 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
207 if_stack[if_stack_pointer].file = input_filename;
208 if_stack[if_stack_pointer].line = lineno;
209 if_stack[if_stack_pointer].needs_warning = 0;
210 if_stack_pointer++;
6d819282
MK
211
212 expand_start_cond (cond, exitflag);
213}
214
0a7ed33c
BS
215/* Generate RTL for the end of an if-then. Optionally warn if a nested
216 if statement had an ambiguous else clause. */
217
6d819282
MK
218void
219c_expand_end_cond ()
220{
221 if_stack_pointer--;
0a7ed33c
BS
222 if (if_stack[if_stack_pointer].needs_warning)
223 warning_with_file_and_line (if_stack[if_stack_pointer].file,
224 if_stack[if_stack_pointer].line,
225 "suggest explicit braces to avoid ambiguous `else'");
6d819282
MK
226 expand_end_cond ();
227}
228
0a7ed33c
BS
229/* Generate RTL between the then-clause and the else-clause
230 of an if-then-else. */
231
6d819282
MK
232void
233c_expand_start_else ()
234{
0a7ed33c
BS
235 /* An ambiguous else warning must be generated for the enclosing if
236 statement, unless we see an else branch for that one, too. */
6d819282
MK
237 if (warn_parentheses
238 && if_stack_pointer > 1
0a7ed33c
BS
239 && (if_stack[if_stack_pointer - 1].compstmt_count
240 == if_stack[if_stack_pointer - 2].compstmt_count))
241 if_stack[if_stack_pointer - 2].needs_warning = 1;
242
243 /* Even if a nested if statement had an else branch, it can't be
244 ambiguous if this one also has an else. So don't warn in that
245 case. Also don't warn for any if statements nested in this else. */
246 if_stack[if_stack_pointer - 1].needs_warning = 0;
247 if_stack[if_stack_pointer - 1].compstmt_count--;
6d819282
MK
248
249 expand_start_else ();
250}
251
6f4d7222 252/* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
7da551a2
RS
253
254void
255declare_function_name ()
256{
dff01034 257 const char *name, *printable_name;
7da551a2
RS
258
259 if (current_function_decl == NULL)
260 {
261 name = "";
262 printable_name = "top level";
263 }
264 else
265 {
6152f876
RS
266 /* Allow functions to be nameless (such as artificial ones). */
267 if (DECL_NAME (current_function_decl))
268 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
269 else
270 name = "";
a1d7ffe3 271 printable_name = (*decl_printable_name) (current_function_decl, 2);
7da551a2
RS
272 }
273
4724b3de
RS
274 declare_hidden_char_array ("__FUNCTION__", name);
275 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
6f4d7222
UD
276 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
277 ISO C 9x standard; instead a new variable is invented. */
278 declare_hidden_char_array ("__func__", name);
4724b3de 279}
97d17ac2 280
4724b3de
RS
281static void
282declare_hidden_char_array (name, value)
dff01034 283 const char *name, *value;
4724b3de
RS
284{
285 tree decl, type, init;
286 int vlen;
7da551a2 287
4724b3de 288 /* If the default size of char arrays isn't big enough for the name,
700942a0 289 or if we want to give warnings for large objects, make a bigger one. */
4724b3de 290 vlen = strlen (value) + 1;
97d17ac2 291 type = char_array_type_node;
a62e870c 292 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
700942a0 293 || warn_larger_than)
97d17ac2 294 type = build_array_type (char_type_node,
4724b3de 295 build_index_type (build_int_2 (vlen, 0)));
7da551a2 296 push_obstacks_nochange ();
4724b3de 297 decl = build_decl (VAR_DECL, get_identifier (name), type);
7da551a2
RS
298 TREE_STATIC (decl) = 1;
299 TREE_READONLY (decl) = 1;
4724b3de 300 TREE_ASM_WRITTEN (decl) = 1;
b9a24ad4 301 DECL_SOURCE_LINE (decl) = 0;
d2eb0876 302 DECL_ARTIFICIAL (decl) = 1;
176e81eb 303 DECL_IN_SYSTEM_HEADER (decl) = 1;
7da551a2 304 DECL_IGNORED_P (decl) = 1;
4724b3de 305 init = build_string (vlen, value);
97d17ac2 306 TREE_TYPE (init) = type;
7da551a2
RS
307 DECL_INITIAL (decl) = init;
308 finish_decl (pushdecl (decl), init, NULL_TREE);
309}
310
b30f223b
RS
311/* Given a chain of STRING_CST nodes,
312 concatenate them into one STRING_CST
313 and give it a suitable array-of-chars data type. */
314
315tree
316combine_strings (strings)
317 tree strings;
318{
319 register tree value, t;
320 register int length = 1;
321 int wide_length = 0;
322 int wide_flag = 0;
323 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
324 int nchars;
325
326 if (TREE_CHAIN (strings))
327 {
328 /* More than one in the chain, so concatenate. */
329 register char *p, *q;
330
331 /* Don't include the \0 at the end of each substring,
332 except for the last one.
333 Count wide strings and ordinary strings separately. */
334 for (t = strings; t; t = TREE_CHAIN (t))
335 {
336 if (TREE_TYPE (t) == wchar_array_type_node)
337 {
338 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
339 wide_flag = 1;
340 }
341 else
342 length += (TREE_STRING_LENGTH (t) - 1);
343 }
344
345 /* If anything is wide, the non-wides will be converted,
346 which makes them take more space. */
347 if (wide_flag)
348 length = length * wchar_bytes + wide_length;
349
350 p = savealloc (length);
351
352 /* Copy the individual strings into the new combined string.
353 If the combined string is wide, convert the chars to ints
354 for any individual strings that are not wide. */
355
356 q = p;
357 for (t = strings; t; t = TREE_CHAIN (t))
358 {
359 int len = (TREE_STRING_LENGTH (t)
360 - ((TREE_TYPE (t) == wchar_array_type_node)
361 ? wchar_bytes : 1));
362 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
363 {
7e2231e7 364 memcpy (q, TREE_STRING_POINTER (t), len);
b30f223b
RS
365 q += len;
366 }
367 else
368 {
369 int i;
370 for (i = 0; i < len; i++)
41bbd14e
JW
371 {
372 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
373 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
374 else
375 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
376 }
b30f223b
RS
377 q += len * wchar_bytes;
378 }
379 }
380 if (wide_flag)
381 {
382 int i;
383 for (i = 0; i < wchar_bytes; i++)
384 *q++ = 0;
385 }
386 else
387 *q = 0;
388
389 value = make_node (STRING_CST);
390 TREE_STRING_POINTER (value) = p;
391 TREE_STRING_LENGTH (value) = length;
b30f223b
RS
392 }
393 else
394 {
395 value = strings;
396 length = TREE_STRING_LENGTH (value);
397 if (TREE_TYPE (value) == wchar_array_type_node)
398 wide_flag = 1;
399 }
400
b57062ca 401 /* Compute the number of elements, for the array type. */
b30f223b
RS
402 nchars = wide_flag ? length / wchar_bytes : length;
403
404 /* Create the array type for the string constant.
405 -Wwrite-strings says make the string constant an array of const char
d9cf7c82
JM
406 so that copying it to a non-const pointer will get a warning.
407 For C++, this is the standard behavior. */
408 if (flag_const_strings
b30f223b
RS
409 && (! flag_traditional && ! flag_writable_strings))
410 {
411 tree elements
412 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
413 1, 0);
414 TREE_TYPE (value)
415 = build_array_type (elements,
416 build_index_type (build_int_2 (nchars - 1, 0)));
417 }
418 else
419 TREE_TYPE (value)
420 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
421 build_index_type (build_int_2 (nchars - 1, 0)));
d9cf7c82
JM
422
423 TREE_READONLY (value) = TREE_CONSTANT (value) = ! flag_writable_strings;
b30f223b
RS
424 TREE_STATIC (value) = 1;
425 return value;
426}
427\f
53065596
RK
428/* To speed up processing of attributes, we maintain an array of
429 IDENTIFIER_NODES and the corresponding attribute types. */
430
431/* Array to hold attribute information. */
432
433static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
434
435static int attrtab_idx = 0;
436
437/* Add an entry to the attribute table above. */
438
439static void
440add_attribute (id, string, min_len, max_len, decl_req)
441 enum attrs id;
dff01034 442 const char *string;
53065596
RK
443 int min_len, max_len;
444 int decl_req;
445{
446 char buf[100];
447
448 attrtab[attrtab_idx].id = id;
449 attrtab[attrtab_idx].name = get_identifier (string);
450 attrtab[attrtab_idx].min = min_len;
451 attrtab[attrtab_idx].max = max_len;
452 attrtab[attrtab_idx++].decl_req = decl_req;
453
454 sprintf (buf, "__%s__", string);
455
456 attrtab[attrtab_idx].id = id;
457 attrtab[attrtab_idx].name = get_identifier (buf);
458 attrtab[attrtab_idx].min = min_len;
459 attrtab[attrtab_idx].max = max_len;
460 attrtab[attrtab_idx++].decl_req = decl_req;
461}
462
463/* Initialize attribute table. */
464
465static void
466init_attributes ()
467{
a89ca5c6 468 add_attribute (A_PACKED, "packed", 0, 0, 0);
bbb1ae01 469 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
2786cbad 470 add_attribute (A_COMMON, "common", 0, 0, 1);
53065596
RK
471 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
472 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
6d819282 473 add_attribute (A_UNUSED, "unused", 0, 0, 0);
53065596
RK
474 add_attribute (A_CONST, "const", 0, 0, 1);
475 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
476 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
477 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
478 add_attribute (A_MODE, "mode", 1, 1, 1);
479 add_attribute (A_SECTION, "section", 1, 1, 1);
480 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
d161fae4 481 add_attribute (A_FORMAT, "format", 3, 3, 1);
0161e8da 482 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
4b8af8d9
JM
483 add_attribute (A_WEAK, "weak", 0, 0, 1);
484 add_attribute (A_ALIAS, "alias", 1, 1, 1);
07417085 485 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
7d384cc0 486 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
53065596
RK
487}
488\f
adfaf194
JM
489/* Default implementation of valid_lang_attribute, below. By default, there
490 are no language-specific attributes. */
491
492static int
493default_valid_lang_attribute (attr_name, attr_args, decl, type)
494 tree attr_name ATTRIBUTE_UNUSED;
495 tree attr_args ATTRIBUTE_UNUSED;
496 tree decl ATTRIBUTE_UNUSED;
497 tree type ATTRIBUTE_UNUSED;
498{
499 return 0;
500}
501
502/* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
503 attribute for either declaration DECL or type TYPE and 0 otherwise. */
504
505int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree))
506 = default_valid_lang_attribute;
507
1228e2a6 508/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
53065596
RK
509 and install them in NODE, which is either a DECL (including a TYPE_DECL)
510 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
0f41302f 511 and declaration modifiers but before the declaration proper. */
b30f223b
RS
512
513void
53065596
RK
514decl_attributes (node, attributes, prefix_attributes)
515 tree node, attributes, prefix_attributes;
b30f223b 516{
a16b4c9c
JW
517 tree decl = 0, type = 0;
518 int is_type = 0;
53065596
RK
519 tree a;
520
521 if (attrtab_idx == 0)
522 init_attributes ();
523
524 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
525 {
526 decl = node;
527 type = TREE_TYPE (decl);
528 is_type = TREE_CODE (node) == TYPE_DECL;
529 }
530 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
531 type = node, is_type = 1;
a2cd7b45 532
e2af664c
NC
533#ifdef PRAGMA_INSERT_ATTRIBUTES
534 /* If the code in c-pragma.c wants to insert some attributes then
535 allow it to do so. Do this before allowing machine back ends to
536 insert attributes, so that they have the opportunity to override
537 anything done here. */
538 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
539#endif
540
f09db6e0
NC
541#ifdef INSERT_ATTRIBUTES
542 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
543#endif
544
eed32833 545 attributes = chainon (prefix_attributes, attributes);
5289b665 546
b30f223b 547 for (a = attributes; a; a = TREE_CHAIN (a))
53065596
RK
548 {
549 tree name = TREE_PURPOSE (a);
550 tree args = TREE_VALUE (a);
551 int i;
552 enum attrs id;
b57062ca 553
53065596
RK
554 for (i = 0; i < attrtab_idx; i++)
555 if (attrtab[i].name == name)
556 break;
557
6eaba4a7 558 if (i == attrtab_idx)
53065596 559 {
adfaf194
JM
560 if (! valid_machine_attribute (name, args, decl, type)
561 && ! (* valid_lang_attribute) (name, args, decl, type))
6eaba4a7
DE
562 warning ("`%s' attribute directive ignored",
563 IDENTIFIER_POINTER (name));
17c1a44f
SC
564 else if (decl != 0)
565 type = TREE_TYPE (decl);
53065596
RK
566 continue;
567 }
568 else if (attrtab[i].decl_req && decl == 0)
569 {
570 warning ("`%s' attribute does not apply to types",
571 IDENTIFIER_POINTER (name));
572 continue;
573 }
574 else if (list_length (args) < attrtab[i].min
575 || list_length (args) > attrtab[i].max)
576 {
577 error ("wrong number of arguments specified for `%s' attribute",
578 IDENTIFIER_POINTER (name));
579 continue;
580 }
581
582 id = attrtab[i].id;
583 switch (id)
584 {
585 case A_PACKED:
1bcf5b08 586 if (is_type)
a89ca5c6
RK
587 TYPE_PACKED (type) = 1;
588 else if (TREE_CODE (decl) == FIELD_DECL)
53065596
RK
589 DECL_PACKED (decl) = 1;
590 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
591 used for DECL_REGISTER. It wouldn't mean anything anyway. */
592 else
593 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
594 break;
595
bbb1ae01
RK
596 case A_NOCOMMON:
597 if (TREE_CODE (decl) == VAR_DECL)
598 DECL_COMMON (decl) = 0;
599 else
600 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
601 break;
602
2786cbad
JM
603 case A_COMMON:
604 if (TREE_CODE (decl) == VAR_DECL)
605 DECL_COMMON (decl) = 1;
606 else
607 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
608 break;
609
53065596
RK
610 case A_NORETURN:
611 if (TREE_CODE (decl) == FUNCTION_DECL)
612 TREE_THIS_VOLATILE (decl) = 1;
613 else if (TREE_CODE (type) == POINTER_TYPE
614 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
b57062ca 615 TREE_TYPE (decl) = type
53065596
RK
616 = build_pointer_type
617 (build_type_variant (TREE_TYPE (type),
618 TREE_READONLY (TREE_TYPE (type)), 1));
619 else
620 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
621 break;
622
1c4fadec 623 case A_UNUSED:
6d819282
MK
624 if (is_type)
625 TREE_USED (type) = 1;
b57062ca 626 else if (TREE_CODE (decl) == PARM_DECL
6d819282 627 || TREE_CODE (decl) == VAR_DECL
736b02fd
KG
628 || TREE_CODE (decl) == FUNCTION_DECL
629 || TREE_CODE (decl) == LABEL_DECL)
1c4fadec
RK
630 TREE_USED (decl) = 1;
631 else
632 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
633 break;
634
53065596
RK
635 case A_CONST:
636 if (TREE_CODE (decl) == FUNCTION_DECL)
637 TREE_READONLY (decl) = 1;
638 else if (TREE_CODE (type) == POINTER_TYPE
639 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
640 TREE_TYPE (decl) = type
641 = build_pointer_type
642 (build_type_variant (TREE_TYPE (type), 1,
643 TREE_THIS_VOLATILE (TREE_TYPE (type))));
644 else
645 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
646 break;
647
648 case A_T_UNION:
1bcf5b08 649 if (is_type
53065596 650 && TREE_CODE (type) == UNION_TYPE
1bcf5b08 651 && (decl == 0
62b1077c
RK
652 || (TYPE_FIELDS (type) != 0
653 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
1bcf5b08
RK
654 TYPE_TRANSPARENT_UNION (type) = 1;
655 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
53065596
RK
656 && TREE_CODE (type) == UNION_TYPE
657 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
1bcf5b08 658 DECL_TRANSPARENT_UNION (decl) = 1;
53065596
RK
659 else
660 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
661 break;
662
663 case A_CONSTRUCTOR:
664 if (TREE_CODE (decl) == FUNCTION_DECL
665 && TREE_CODE (type) == FUNCTION_TYPE
666 && decl_function_context (decl) == 0)
1a16a053
RK
667 {
668 DECL_STATIC_CONSTRUCTOR (decl) = 1;
669 TREE_USED (decl) = 1;
670 }
53065596
RK
671 else
672 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
673 break;
674
675 case A_DESTRUCTOR:
676 if (TREE_CODE (decl) == FUNCTION_DECL
677 && TREE_CODE (type) == FUNCTION_TYPE
678 && decl_function_context (decl) == 0)
1a16a053
RK
679 {
680 DECL_STATIC_DESTRUCTOR (decl) = 1;
681 TREE_USED (decl) = 1;
682 }
53065596
RK
683 else
684 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
685 break;
686
687 case A_MODE:
688 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
689 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
690 else
691 {
692 int j;
dff01034 693 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
53065596
RK
694 int len = strlen (p);
695 enum machine_mode mode = VOIDmode;
696 tree typefm;
697
698 if (len > 4 && p[0] == '_' && p[1] == '_'
699 && p[len - 1] == '_' && p[len - 2] == '_')
700 {
3438dff9 701 char *newp = (char *) alloca (len - 1);
53065596
RK
702
703 strcpy (newp, &p[2]);
704 newp[len - 4] = '\0';
705 p = newp;
706 }
707
708 /* Give this decl a type with the specified mode.
709 First check for the special modes. */
710 if (! strcmp (p, "byte"))
711 mode = byte_mode;
712 else if (!strcmp (p, "word"))
713 mode = word_mode;
714 else if (! strcmp (p, "pointer"))
715 mode = ptr_mode;
716 else
717 for (j = 0; j < NUM_MACHINE_MODES; j++)
718 if (!strcmp (p, GET_MODE_NAME (j)))
719 mode = (enum machine_mode) j;
720
721 if (mode == VOIDmode)
722 error ("unknown machine mode `%s'", p);
723 else if (0 == (typefm = type_for_mode (mode,
724 TREE_UNSIGNED (type))))
725 error ("no data type for mode `%s'", p);
726 else
727 {
728 TREE_TYPE (decl) = type = typefm;
729 DECL_SIZE (decl) = 0;
730 layout_decl (decl, 0);
731 }
732 }
733 break;
734
735 case A_SECTION:
5289b665 736#ifdef ASM_OUTPUT_SECTION_NAME
53065596
RK
737 if ((TREE_CODE (decl) == FUNCTION_DECL
738 || TREE_CODE (decl) == VAR_DECL)
739 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
740 {
b57062ca 741 if (TREE_CODE (decl) == VAR_DECL
f4ca236c
RK
742 && current_function_decl != NULL_TREE
743 && ! TREE_STATIC (decl))
53065596
RK
744 error_with_decl (decl,
745 "section attribute cannot be specified for local variables");
746 /* The decl may have already been given a section attribute from
747 a previous declaration. Ensure they match. */
748 else if (DECL_SECTION_NAME (decl) != NULL_TREE
749 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
750 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
751 error_with_decl (node,
752 "section of `%s' conflicts with previous declaration");
753 else
754 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
755 }
756 else
757 error_with_decl (node,
5289b665
DE
758 "section attribute not allowed for `%s'");
759#else
53065596
RK
760 error_with_decl (node,
761 "section attributes are not supported for this target");
5289b665 762#endif
53065596
RK
763 break;
764
765 case A_ALIGNED:
6f38f669 766 {
53065596 767 tree align_expr
54630035
RK
768 = (args ? TREE_VALUE (args)
769 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
53065596 770 int align;
f09db6e0 771
53065596
RK
772 /* Strip any NOPs of any kind. */
773 while (TREE_CODE (align_expr) == NOP_EXPR
774 || TREE_CODE (align_expr) == CONVERT_EXPR
775 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
776 align_expr = TREE_OPERAND (align_expr, 0);
b57062ca 777
53065596
RK
778 if (TREE_CODE (align_expr) != INTEGER_CST)
779 {
780 error ("requested alignment is not a constant");
781 continue;
782 }
783
784 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
785
786 if (exact_log2 (align) == -1)
787 error ("requested alignment is not a power of 2");
788 else if (is_type)
06aa52de 789 TYPE_ALIGN (type) = align;
53065596
RK
790 else if (TREE_CODE (decl) != VAR_DECL
791 && TREE_CODE (decl) != FIELD_DECL)
792 error_with_decl (decl,
793 "alignment may not be specified for `%s'");
794 else
795 DECL_ALIGN (decl) = align;
6f38f669 796 }
53065596 797 break;
6f38f669 798
53065596 799 case A_FORMAT:
b30f223b 800 {
bb72a084 801 tree format_type_id = TREE_VALUE (args);
53065596
RK
802 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
803 tree first_arg_num_expr
804 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
805 int format_num;
806 int first_arg_num;
bb72a084 807 enum format_type format_type;
53065596
RK
808 tree argument;
809 int arg_num;
b57062ca 810
53065596
RK
811 if (TREE_CODE (decl) != FUNCTION_DECL)
812 {
813 error_with_decl (decl,
814 "argument format specified for non-function `%s'");
815 continue;
816 }
f5963e61 817
bb72a084 818 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
0161e8da 819 {
bb72a084 820 error ("unrecognized format specifier");
0161e8da
RK
821 continue;
822 }
53065596
RK
823 else
824 {
dff01034 825 const char *p = IDENTIFIER_POINTER (format_type_id);
f5963e61 826
bb72a084
PE
827 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
828 format_type = printf_format_type;
829 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
830 format_type = scanf_format_type;
831 else if (!strcmp (p, "strftime")
832 || !strcmp (p, "__strftime__"))
833 format_type = strftime_format_type;
834 else
835 {
b27d2bd5 836 warning ("`%s' is an unrecognized format function type", p);
bb72a084
PE
837 continue;
838 }
53065596 839 }
6f38f669 840
53065596
RK
841 /* Strip any conversions from the string index and first arg number
842 and verify they are constants. */
843 while (TREE_CODE (format_num_expr) == NOP_EXPR
844 || TREE_CODE (format_num_expr) == CONVERT_EXPR
845 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
846 format_num_expr = TREE_OPERAND (format_num_expr, 0);
677ff441 847
53065596
RK
848 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
849 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
850 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
851 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
852
853 if (TREE_CODE (format_num_expr) != INTEGER_CST
854 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
09e3dd72 855 {
53065596
RK
856 error ("format string has non-constant operand number");
857 continue;
09e3dd72 858 }
53065596
RK
859
860 format_num = TREE_INT_CST_LOW (format_num_expr);
861 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
862 if (first_arg_num != 0 && first_arg_num <= format_num)
68a91d8d 863 {
53065596 864 error ("format string arg follows the args to be formatted");
6f38f669 865 continue;
68a91d8d 866 }
53065596
RK
867
868 /* If a parameter list is specified, verify that the format_num
869 argument is actually a string, in case the format attribute
870 is in error. */
871 argument = TYPE_ARG_TYPES (type);
872 if (argument)
09e3dd72 873 {
53065596 874 for (arg_num = 1; ; ++arg_num)
09e3dd72 875 {
53065596
RK
876 if (argument == 0 || arg_num == format_num)
877 break;
878 argument = TREE_CHAIN (argument);
879 }
880 if (! argument
881 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
882 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
883 != char_type_node))
884 {
885 error ("format string arg not a string type");
09e3dd72
RK
886 continue;
887 }
53065596
RK
888 if (first_arg_num != 0)
889 {
890 /* Verify that first_arg_num points to the last arg,
0f41302f 891 the ... */
53065596
RK
892 while (argument)
893 arg_num++, argument = TREE_CHAIN (argument);
894 if (arg_num != first_arg_num)
895 {
896 error ("args to be formatted is not ...");
897 continue;
898 }
899 }
09e3dd72 900 }
a2cd7b45 901
53065596
RK
902 record_function_format (DECL_NAME (decl),
903 DECL_ASSEMBLER_NAME (decl),
bb72a084 904 format_type, format_num, first_arg_num);
53065596
RK
905 break;
906 }
4b8af8d9 907
0161e8da
RK
908 case A_FORMAT_ARG:
909 {
910 tree format_num_expr = TREE_VALUE (args);
911 int format_num, arg_num;
912 tree argument;
b57062ca 913
0161e8da
RK
914 if (TREE_CODE (decl) != FUNCTION_DECL)
915 {
916 error_with_decl (decl,
917 "argument format specified for non-function `%s'");
918 continue;
919 }
b57062ca 920
0161e8da
RK
921 /* Strip any conversions from the first arg number and verify it
922 is a constant. */
923 while (TREE_CODE (format_num_expr) == NOP_EXPR
924 || TREE_CODE (format_num_expr) == CONVERT_EXPR
925 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
926 format_num_expr = TREE_OPERAND (format_num_expr, 0);
927
928 if (TREE_CODE (format_num_expr) != INTEGER_CST)
929 {
930 error ("format string has non-constant operand number");
931 continue;
932 }
933
934 format_num = TREE_INT_CST_LOW (format_num_expr);
935
936 /* If a parameter list is specified, verify that the format_num
937 argument is actually a string, in case the format attribute
938 is in error. */
939 argument = TYPE_ARG_TYPES (type);
940 if (argument)
941 {
942 for (arg_num = 1; ; ++arg_num)
943 {
944 if (argument == 0 || arg_num == format_num)
945 break;
946 argument = TREE_CHAIN (argument);
947 }
948 if (! argument
949 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
950 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
951 != char_type_node))
952 {
953 error ("format string arg not a string type");
954 continue;
955 }
956 }
957
958 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
959 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
960 != char_type_node))
961 {
962 error ("function does not return string type");
963 continue;
964 }
965
966 record_international_format (DECL_NAME (decl),
967 DECL_ASSEMBLER_NAME (decl),
968 format_num);
969 break;
970 }
971
4b8af8d9
JM
972 case A_WEAK:
973 declare_weak (decl);
974 break;
975
976 case A_ALIAS:
977 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
5d9dd5a5 978 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
4b8af8d9
JM
979 error_with_decl (decl,
980 "`%s' defined both normally and as an alias");
981 else if (decl_function_context (decl) == 0)
982 {
20045452
RH
983 tree id;
984
985 id = TREE_VALUE (args);
986 if (TREE_CODE (id) != STRING_CST)
987 {
988 error ("alias arg not a string");
989 break;
990 }
991 id = get_identifier (TREE_STRING_POINTER (id));
992
4b8af8d9
JM
993 if (TREE_CODE (decl) == FUNCTION_DECL)
994 DECL_INITIAL (decl) = error_mark_node;
995 else
996 DECL_EXTERNAL (decl) = 0;
997 assemble_alias (decl, id);
998 }
999 else
1000 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1001 break;
07417085 1002
7d384cc0
KR
1003 case A_NO_CHECK_MEMORY_USAGE:
1004 if (TREE_CODE (decl) != FUNCTION_DECL)
1005 {
1006 error_with_decl (decl,
1007 "`%s' attribute applies only to functions",
1008 IDENTIFIER_POINTER (name));
1009 }
1010 else if (DECL_INITIAL (decl))
1011 {
1012 error_with_decl (decl,
1013 "can't set `%s' attribute after definition",
1014 IDENTIFIER_POINTER (name));
1015 }
1016 else
1017 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1018 break;
1019
07417085
KR
1020 case A_NO_INSTRUMENT_FUNCTION:
1021 if (TREE_CODE (decl) != FUNCTION_DECL)
1022 {
1023 error_with_decl (decl,
1024 "`%s' attribute applies only to functions",
1025 IDENTIFIER_POINTER (name));
1026 }
1027 else if (DECL_INITIAL (decl))
1028 {
1029 error_with_decl (decl,
1030 "can't set `%s' attribute after definition",
1031 IDENTIFIER_POINTER (name));
1032 }
1033 else
1034 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1035 break;
53065596
RK
1036 }
1037 }
b30f223b 1038}
800f4153
RK
1039
1040/* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1041 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1042
1043 The head of the declspec list is stored in DECLSPECS.
1044 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1045
1046 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1047 the list elements. We drop the containing TREE_LIST nodes and link the
1048 resulting attributes together the way decl_attributes expects them. */
1049
1050void
1051split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1052 tree specs_attrs;
1053 tree *declspecs, *prefix_attributes;
1054{
1055 tree t, s, a, next, specs, attrs;
1056
1057 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1058 if (specs_attrs != NULL_TREE
1059 && TREE_CODE (specs_attrs) != TREE_LIST)
1060 {
1061 *declspecs = specs_attrs;
1062 *prefix_attributes = NULL_TREE;
1063 return;
1064 }
1065
1066 /* Remember to keep the lists in the same order, element-wise. */
1067
1068 specs = s = NULL_TREE;
1069 attrs = a = NULL_TREE;
1070 for (t = specs_attrs; t; t = next)
1071 {
1072 next = TREE_CHAIN (t);
1073 /* Declspecs have a non-NULL TREE_VALUE. */
1074 if (TREE_VALUE (t) != NULL_TREE)
1075 {
1076 if (specs == NULL_TREE)
1077 specs = s = t;
1078 else
1079 {
1080 TREE_CHAIN (s) = t;
1081 s = t;
1082 }
1083 }
1084 else
1085 {
1086 if (attrs == NULL_TREE)
1087 attrs = a = TREE_PURPOSE (t);
1088 else
1089 {
1090 TREE_CHAIN (a) = TREE_PURPOSE (t);
1091 a = TREE_PURPOSE (t);
1092 }
1093 /* More attrs can be linked here, move A to the end. */
1094 while (TREE_CHAIN (a) != NULL_TREE)
1095 a = TREE_CHAIN (a);
1096 }
1097 }
1098
1099 /* Terminate the lists. */
1100 if (s != NULL_TREE)
1101 TREE_CHAIN (s) = NULL_TREE;
1102 if (a != NULL_TREE)
1103 TREE_CHAIN (a) = NULL_TREE;
1104
1105 /* All done. */
1106 *declspecs = specs;
1107 *prefix_attributes = attrs;
1108}
d9525bec
BK
1109
1110/* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1111 This function is used by the parser when a rule will accept attributes
1112 in a particular position, but we don't want to support that just yet.
1113
1114 A warning is issued for every ignored attribute. */
1115
1116tree
1117strip_attrs (specs_attrs)
1118 tree specs_attrs;
1119{
1120 tree specs, attrs;
1121
1122 split_specs_attrs (specs_attrs, &specs, &attrs);
1123
1124 while (attrs)
1125 {
1126 warning ("`%s' attribute ignored",
1127 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1128 attrs = TREE_CHAIN (attrs);
1129 }
1130
1131 return specs;
1132}
b30f223b 1133\f
1ccf251f
RK
1134/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1135 a parameter list. */
1136
1137#define T_I &integer_type_node
1138#define T_L &long_integer_type_node
2314fb70 1139#define T_LL &long_long_integer_type_node
1ccf251f
RK
1140#define T_S &short_integer_type_node
1141#define T_UI &unsigned_type_node
1142#define T_UL &long_unsigned_type_node
2314fb70 1143#define T_ULL &long_long_unsigned_type_node
1ccf251f
RK
1144#define T_US &short_unsigned_type_node
1145#define T_F &float_type_node
1146#define T_D &double_type_node
1147#define T_LD &long_double_type_node
1148#define T_C &char_type_node
b57062ca 1149#define T_UC &unsigned_char_type_node
1ccf251f
RK
1150#define T_V &void_type_node
1151#define T_W &wchar_type_node
df8a401a 1152#define T_ST &sizetype
1ccf251f
RK
1153
1154typedef struct {
dff01034 1155 const char *format_chars;
1ccf251f
RK
1156 int pointer_count;
1157 /* Type of argument if no length modifier is used. */
1158 tree *nolen;
bc516719 1159 /* Type of argument if length modifier for shortening to byte is used.
1ccf251f 1160 If NULL, then this modifier is not allowed. */
b57062ca 1161 tree *hhlen;
bc516719 1162 /* Type of argument if length modifier for shortening is used.
b57062ca 1163 If NULL, then this modifier is not allowed. */
1ccf251f
RK
1164 tree *hlen;
1165 /* Type of argument if length modifier `l' is used.
1166 If NULL, then this modifier is not allowed. */
1167 tree *llen;
2cedb812 1168 /* Type of argument if length modifier `q' or `ll' is used.
2314fb70
CH
1169 If NULL, then this modifier is not allowed. */
1170 tree *qlen;
1ccf251f
RK
1171 /* Type of argument if length modifier `L' is used.
1172 If NULL, then this modifier is not allowed. */
1173 tree *bigllen;
e5e809f4
JL
1174 /* Type of argument if length modifier `Z' is used.
1175 If NULL, then this modifier is not allowed. */
1176 tree *zlen;
1ccf251f 1177 /* List of other modifier characters allowed with these options. */
dff01034 1178 const char *flag_chars;
1ccf251f
RK
1179} format_char_info;
1180
1181static format_char_info print_char_table[] = {
b57062ca
UD
1182 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1183 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1184 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
e5e809f4 1185/* A GNU extension. */
b57062ca
UD
1186 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1187 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1188 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1189 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1190 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1191 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1192 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1193 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
c84e2712 1194 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1ccf251f
RK
1195};
1196
1197static format_char_info scan_char_table[] = {
b57062ca
UD
1198 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1199 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1200 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
c6a9dea8
JW
1201 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1202 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
b57062ca
UD
1203 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1204 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
c6a9dea8 1205 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
b57062ca
UD
1206 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1207 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
c84e2712 1208 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1ccf251f
RK
1209};
1210
bb72a084
PE
1211/* Handle format characters recognized by glibc's strftime.c.
1212 '2' - MUST do years as only two digits
1213 '3' - MAY do years as only two digits (depending on locale)
1214 'E' - E modifier is acceptable
1215 'O' - O modifier is acceptable to Standard C
1216 'o' - O modifier is acceptable as a GNU extension
1217 'G' - other GNU extensions */
1218
1219static format_char_info time_char_table[] = {
b57062ca
UD
1220 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1221 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1222 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1223 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1224 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1225 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1226 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1227 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1228 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1229 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1230 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1231 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1232 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
c84e2712 1233 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
bb72a084
PE
1234};
1235
0161e8da
RK
1236typedef struct function_format_info
1237{
1ccf251f
RK
1238 struct function_format_info *next; /* next structure on the list */
1239 tree name; /* identifier such as "printf" */
1240 tree assembler_name; /* optional mangled identifier (for C++) */
bb72a084 1241 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1ccf251f
RK
1242 int format_num; /* number of format argument */
1243 int first_arg_num; /* number of first arg (zero for varargs) */
1244} function_format_info;
1245
1246static function_format_info *function_format_list = NULL;
1247
0161e8da
RK
1248typedef struct international_format_info
1249{
1250 struct international_format_info *next; /* next structure on the list */
1251 tree name; /* identifier such as "gettext" */
1252 tree assembler_name; /* optional mangled identifier (for C++) */
1253 int format_num; /* number of format argument */
1254} international_format_info;
1255
1256static international_format_info *international_format_list = NULL;
1257
1258static void check_format_info PROTO((function_format_info *, tree));
1ccf251f
RK
1259
1260/* Initialize the table of functions to perform format checking on.
1261 The ANSI functions are always checked (whether <stdio.h> is
1262 included or not), since it is common to call printf without
1263 including <stdio.h>. There shouldn't be a problem with this,
1264 since ANSI reserves these function names whether you include the
b57062ca 1265 header file or not. In any case, the checking is harmless.
0161e8da
RK
1266
1267 Also initialize the name of function that modify the format string for
1268 internationalization purposes. */
1ccf251f
RK
1269
1270void
1271init_function_format_info ()
1272{
bb72a084
PE
1273 record_function_format (get_identifier ("printf"), NULL_TREE,
1274 printf_format_type, 1, 2);
1275 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1276 printf_format_type, 2, 3);
1277 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1278 printf_format_type, 2, 3);
1279 record_function_format (get_identifier ("scanf"), NULL_TREE,
1280 scanf_format_type, 1, 2);
1281 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1282 scanf_format_type, 2, 3);
1283 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1284 scanf_format_type, 2, 3);
1285 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1286 printf_format_type, 1, 0);
1287 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1288 printf_format_type, 2, 0);
1289 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1290 printf_format_type, 2, 0);
1291 record_function_format (get_identifier ("strftime"), NULL_TREE,
1292 strftime_format_type, 3, 0);
0161e8da
RK
1293
1294 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1295 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1296 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1ccf251f
RK
1297}
1298
1299/* Record information for argument format checking. FUNCTION_IDENT is
1300 the identifier node for the name of the function to check (its decl
bb72a084
PE
1301 need not exist yet).
1302 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1ccf251f
RK
1303 of the argument which is the format control string (starting from 1).
1304 FIRST_ARG_NUM is the number of the first actual argument to check
0161e8da 1305 against the format string, or zero if no checking is not be done
1ccf251f
RK
1306 (e.g. for varargs such as vfprintf). */
1307
bb72a084
PE
1308static void
1309record_function_format (name, assembler_name, format_type,
1ccf251f
RK
1310 format_num, first_arg_num)
1311 tree name;
1312 tree assembler_name;
bb72a084 1313 enum format_type format_type;
1ccf251f
RK
1314 int format_num;
1315 int first_arg_num;
1316{
1317 function_format_info *info;
1318
1319 /* Re-use existing structure if it's there. */
1320
1321 for (info = function_format_list; info; info = info->next)
1322 {
1323 if (info->name == name && info->assembler_name == assembler_name)
1324 break;
1325 }
1326 if (! info)
1327 {
1328 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1329 info->next = function_format_list;
1330 function_format_list = info;
1331
1332 info->name = name;
1333 info->assembler_name = assembler_name;
1334 }
1335
bb72a084 1336 info->format_type = format_type;
1ccf251f
RK
1337 info->format_num = format_num;
1338 info->first_arg_num = first_arg_num;
1339}
1340
0161e8da
RK
1341/* Record information for the names of function that modify the format
1342 argument to format functions. FUNCTION_IDENT is the identifier node for
1343 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1344 the number of the argument which is the format control string (starting
1345 from 1). */
1346
31837ce2 1347static void
0161e8da
RK
1348record_international_format (name, assembler_name, format_num)
1349 tree name;
1350 tree assembler_name;
1351 int format_num;
1352{
1353 international_format_info *info;
1354
1355 /* Re-use existing structure if it's there. */
1356
1357 for (info = international_format_list; info; info = info->next)
1358 {
1359 if (info->name == name && info->assembler_name == assembler_name)
1360 break;
1361 }
1362
1363 if (! info)
1364 {
1365 info
1366 = (international_format_info *)
1367 xmalloc (sizeof (international_format_info));
1368 info->next = international_format_list;
1369 international_format_list = info;
1370
1371 info->name = name;
1372 info->assembler_name = assembler_name;
1373 }
1374
1375 info->format_num = format_num;
1376}
1377
ab87f8c8
JL
1378static void
1379tfaff ()
1380{
1381 warning ("too few arguments for format");
1382}
1ccf251f
RK
1383\f
1384/* Check the argument list of a call to printf, scanf, etc.
1385 NAME is the function identifier.
1386 ASSEMBLER_NAME is the function's assembler identifier.
1387 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1388 PARAMS is the list of argument values. */
1389
1390void
1391check_function_format (name, assembler_name, params)
1392 tree name;
1393 tree assembler_name;
1394 tree params;
1395{
1396 function_format_info *info;
1397
1398 /* See if this function is a format function. */
1399 for (info = function_format_list; info; info = info->next)
1400 {
95216dec 1401 if (info->assembler_name
1ccf251f
RK
1402 ? (info->assembler_name == assembler_name)
1403 : (info->name == name))
1404 {
1405 /* Yup; check it. */
1406 check_format_info (info, params);
1407 break;
1408 }
1409 }
1410}
1411
1412/* Check the argument list of a call to printf, scanf, etc.
1413 INFO points to the function_format_info structure.
1414 PARAMS is the list of argument values. */
1415
1416static void
1417check_format_info (info, params)
1418 function_format_info *info;
1419 tree params;
1420{
1421 int i;
1422 int arg_num;
1423 int suppressed, wide, precise;
f67aab2c 1424 int length_char = 0;
1ccf251f
RK
1425 int format_char;
1426 int format_length;
1427 tree format_tree;
1428 tree cur_param;
1429 tree cur_type;
1430 tree wanted_type;
9b69f523 1431 tree first_fillin_param;
dff01034 1432 const char *format_chars;
f67aab2c 1433 format_char_info *fci = NULL;
1ccf251f 1434 char flag_chars[8];
9b69f523 1435 int has_operand_number = 0;
1ccf251f
RK
1436
1437 /* Skip to format argument. If the argument isn't available, there's
1438 no work for us to do; prototype checking will catch the problem. */
1439 for (arg_num = 1; ; ++arg_num)
1440 {
1441 if (params == 0)
1442 return;
1443 if (arg_num == info->format_num)
1444 break;
1445 params = TREE_CHAIN (params);
1446 }
1447 format_tree = TREE_VALUE (params);
1448 params = TREE_CHAIN (params);
1449 if (format_tree == 0)
1450 return;
0161e8da 1451
1ccf251f
RK
1452 /* We can only check the format if it's a string constant. */
1453 while (TREE_CODE (format_tree) == NOP_EXPR)
1454 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
0161e8da
RK
1455
1456 if (TREE_CODE (format_tree) == CALL_EXPR
1457 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1458 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1459 == FUNCTION_DECL))
1460 {
1461 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1462
1463 /* See if this is a call to a known internationalization function
1464 that modifies the format arg. */
1465 international_format_info *info;
1466
1467 for (info = international_format_list; info; info = info->next)
1468 if (info->assembler_name
1469 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1470 : (info->name == DECL_NAME (function)))
1471 {
1472 tree inner_args;
1473 int i;
1474
1475 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1476 inner_args != 0;
1477 inner_args = TREE_CHAIN (inner_args), i++)
1478 if (i == info->format_num)
1479 {
1480 format_tree = TREE_VALUE (inner_args);
1481
1482 while (TREE_CODE (format_tree) == NOP_EXPR)
1483 format_tree = TREE_OPERAND (format_tree, 0);
1484 }
1485 }
1486 }
1487
39b751ce 1488 if (integer_zerop (format_tree))
1ccf251f
RK
1489 {
1490 warning ("null format string");
1491 return;
1492 }
1493 if (TREE_CODE (format_tree) != ADDR_EXPR)
1494 return;
1495 format_tree = TREE_OPERAND (format_tree, 0);
1496 if (TREE_CODE (format_tree) != STRING_CST)
1497 return;
1498 format_chars = TREE_STRING_POINTER (format_tree);
1499 format_length = TREE_STRING_LENGTH (format_tree);
1500 if (format_length <= 1)
1501 warning ("zero-length format string");
1502 if (format_chars[--format_length] != 0)
1503 {
1504 warning ("unterminated format string");
1505 return;
1506 }
1507 /* Skip to first argument to check. */
1508 while (arg_num + 1 < info->first_arg_num)
1509 {
1510 if (params == 0)
1511 return;
1512 params = TREE_CHAIN (params);
1513 ++arg_num;
1514 }
9b69f523
RK
1515
1516 first_fillin_param = params;
1ccf251f
RK
1517 while (1)
1518 {
af3c5588 1519 int aflag;
1ccf251f
RK
1520 if (*format_chars == 0)
1521 {
1522 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1523 warning ("embedded `\\0' in format");
9b69f523 1524 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1ccf251f
RK
1525 warning ("too many arguments for format");
1526 return;
1527 }
1528 if (*format_chars++ != '%')
1529 continue;
1530 if (*format_chars == 0)
1531 {
1532 warning ("spurious trailing `%%' in format");
1533 continue;
1534 }
1535 if (*format_chars == '%')
1536 {
1537 ++format_chars;
1538 continue;
1539 }
1540 flag_chars[0] = 0;
1541 suppressed = wide = precise = FALSE;
bb72a084 1542 if (info->format_type == scanf_format_type)
1ccf251f
RK
1543 {
1544 suppressed = *format_chars == '*';
1545 if (suppressed)
1546 ++format_chars;
e9a780ec 1547 while (ISDIGIT (*format_chars))
1ccf251f
RK
1548 ++format_chars;
1549 }
bb72a084
PE
1550 else if (info->format_type == strftime_format_type)
1551 {
1552 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1553 {
1554 if (pedantic)
1555 warning ("ANSI C does not support the strftime `%c' flag",
1556 *format_chars);
1557 if (index (flag_chars, *format_chars) != 0)
1558 {
1559 warning ("repeated `%c' flag in format",
1560 *format_chars);
1561 ++format_chars;
1562 }
1563 else
1564 {
1565 i = strlen (flag_chars);
1566 flag_chars[i++] = *format_chars++;
1567 flag_chars[i] = 0;
1568 }
1569 }
e9a780ec 1570 while (ISDIGIT ((unsigned char) *format_chars))
bb72a084
PE
1571 {
1572 wide = TRUE;
1573 ++format_chars;
1574 }
1575 if (wide && pedantic)
1576 warning ("ANSI C does not support strftime format width");
1577 if (*format_chars == 'E' || *format_chars == 'O')
1578 {
1579 i = strlen (flag_chars);
1580 flag_chars[i++] = *format_chars++;
1581 flag_chars[i] = 0;
1582 if (*format_chars == 'E' || *format_chars == 'O')
1583 {
1584 warning ("multiple E/O modifiers in format");
1585 while (*format_chars == 'E' || *format_chars == 'O')
1586 ++format_chars;
1587 }
1588 }
1589 }
1590 else if (info->format_type == printf_format_type)
1ccf251f 1591 {
9b69f523
RK
1592 /* See if we have a number followed by a dollar sign. If we do,
1593 it is an operand number, so set PARAMS to that operand. */
1594 if (*format_chars >= '0' && *format_chars <= '9')
1595 {
dff01034 1596 const char *p = format_chars;
9b69f523
RK
1597
1598 while (*p >= '0' && *p++ <= '9')
1599 ;
1600
1601 if (*p == '$')
1602 {
1603 int opnum = atoi (format_chars);
1604
1605 params = first_fillin_param;
1606 format_chars = p + 1;
1607 has_operand_number = 1;
1608
1609 for (i = 1; i < opnum && params != 0; i++)
1610 params = TREE_CHAIN (params);
1611
1612 if (opnum == 0 || params == 0)
1613 {
1614 warning ("operand number out of range in format");
1615 return;
1616 }
1617 }
1618 }
1619
1ccf251f
RK
1620 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1621 {
1622 if (index (flag_chars, *format_chars) != 0)
bb72a084 1623 warning ("repeated `%c' flag in format", *format_chars++);
6d819282
MK
1624 else
1625 {
1626 i = strlen (flag_chars);
1627 flag_chars[i++] = *format_chars++;
1628 flag_chars[i] = 0;
1629 }
1ccf251f 1630 }
b57062ca 1631 /* "If the space and + flags both appear,
1ccf251f
RK
1632 the space flag will be ignored." */
1633 if (index (flag_chars, ' ') != 0
1634 && index (flag_chars, '+') != 0)
1635 warning ("use of both ` ' and `+' flags in format");
1636 /* "If the 0 and - flags both appear,
1637 the 0 flag will be ignored." */
1638 if (index (flag_chars, '0') != 0
1639 && index (flag_chars, '-') != 0)
1640 warning ("use of both `0' and `-' flags in format");
1641 if (*format_chars == '*')
1642 {
1643 wide = TRUE;
1644 /* "...a field width...may be indicated by an asterisk.
1645 In this case, an int argument supplies the field width..." */
1646 ++format_chars;
1647 if (params == 0)
1648 {
ab87f8c8 1649 tfaff ();
1ccf251f
RK
1650 return;
1651 }
1652 if (info->first_arg_num != 0)
1653 {
1654 cur_param = TREE_VALUE (params);
1655 params = TREE_CHAIN (params);
1656 ++arg_num;
1657 /* size_t is generally not valid here.
1658 It will work on most machines, because size_t and int
1659 have the same mode. But might as well warn anyway,
1660 since it will fail on other machines. */
309ffab6
RS
1661 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1662 != integer_type_node)
1663 &&
1664 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1665 != unsigned_type_node))
bb72a084 1666 warning ("field width is not type int (arg %d)", arg_num);
1ccf251f
RK
1667 }
1668 }
1669 else
1670 {
e9a780ec 1671 while (ISDIGIT (*format_chars))
1ccf251f
RK
1672 {
1673 wide = TRUE;
1674 ++format_chars;
1675 }
1676 }
1677 if (*format_chars == '.')
1678 {
1679 precise = TRUE;
1680 ++format_chars;
e9a780ec 1681 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1ccf251f
RK
1682 warning ("`.' not followed by `*' or digit in format");
1683 /* "...a...precision...may be indicated by an asterisk.
1684 In this case, an int argument supplies the...precision." */
1685 if (*format_chars == '*')
1686 {
1687 if (info->first_arg_num != 0)
1688 {
1689 ++format_chars;
1690 if (params == 0)
1691 {
ab87f8c8 1692 tfaff ();
1ccf251f
RK
1693 return;
1694 }
1695 cur_param = TREE_VALUE (params);
1696 params = TREE_CHAIN (params);
1697 ++arg_num;
1698 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1699 != integer_type_node)
bb72a084
PE
1700 warning ("field width is not type int (arg %d)",
1701 arg_num);
1ccf251f
RK
1702 }
1703 }
1704 else
1705 {
e9a780ec 1706 while (ISDIGIT (*format_chars))
1ccf251f
RK
1707 ++format_chars;
1708 }
1709 }
1710 }
bb72a084 1711
af3c5588 1712 aflag = 0;
bb72a084
PE
1713
1714 if (info->format_type != strftime_format_type)
af3c5588 1715 {
bb72a084
PE
1716 if (*format_chars == 'h' || *format_chars == 'l')
1717 length_char = *format_chars++;
1718 else if (*format_chars == 'q' || *format_chars == 'L')
6d819282 1719 {
bb72a084 1720 length_char = *format_chars++;
f5963e61 1721 if (pedantic)
bb72a084
PE
1722 warning ("ANSI C does not support the `%c' length modifier",
1723 length_char);
6d819282 1724 }
bb72a084
PE
1725 else if (*format_chars == 'Z')
1726 {
1727 length_char = *format_chars++;
1728 if (pedantic)
1729 warning ("ANSI C does not support the `Z' length modifier");
1730 }
1731 else
1732 length_char = 0;
1733 if (length_char == 'l' && *format_chars == 'l')
1734 {
1735 length_char = 'q', format_chars++;
b57062ca 1736 /* FIXME: Is allowed in ISO C 9x. */
bb72a084
PE
1737 if (pedantic)
1738 warning ("ANSI C does not support the `ll' length modifier");
1739 }
bc516719
AS
1740 else if (length_char == 'h' && *format_chars == 'h')
1741 {
1742 length_char = 'H', format_chars++;
1743 /* FIXME: Is allowed in ISO C 9x. */
1744 if (pedantic)
1745 warning ("ANSI C does not support the `hh' length modifier");
1746 }
bb72a084
PE
1747 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1748 {
1749 if (format_chars[1] == 's' || format_chars[1] == 'S'
1750 || format_chars[1] == '[')
1751 {
1752 /* `a' is used as a flag. */
1753 aflag = 1;
1754 format_chars++;
1755 }
1756 }
1757 if (suppressed && length_char != 0)
1758 warning ("use of `*' and `%c' together in format", length_char);
1ccf251f
RK
1759 }
1760 format_char = *format_chars;
bb72a084
PE
1761 if (format_char == 0
1762 || (info->format_type != strftime_format_type && format_char == '%'))
1ccf251f
RK
1763 {
1764 warning ("conversion lacks type at end of format");
1765 continue;
1766 }
c6a9dea8
JW
1767 /* The m, C, and S formats are GNU extensions. */
1768 if (pedantic && info->format_type != strftime_format_type
1769 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1770 warning ("ANSI C does not support the `%c' format", format_char);
1771 /* ??? The a and A formats are C9X extensions, and should be allowed
1772 when a C9X option is added. */
1773 if (pedantic && info->format_type != strftime_format_type
1774 && (format_char == 'a' || format_char == 'A'))
1775 warning ("ANSI C does not support the `%c' format", format_char);
1ccf251f 1776 format_chars++;
bb72a084
PE
1777 switch (info->format_type)
1778 {
1779 case printf_format_type:
1780 fci = print_char_table;
1781 break;
1782 case scanf_format_type:
1783 fci = scan_char_table;
1784 break;
1785 case strftime_format_type:
1786 fci = time_char_table;
1787 break;
1788 default:
1789 abort ();
1790 }
1ccf251f
RK
1791 while (fci->format_chars != 0
1792 && index (fci->format_chars, format_char) == 0)
1793 ++fci;
1794 if (fci->format_chars == 0)
1795 {
1796 if (format_char >= 040 && format_char < 0177)
bb72a084 1797 warning ("unknown conversion type character `%c' in format",
1ccf251f
RK
1798 format_char);
1799 else
bb72a084 1800 warning ("unknown conversion type character 0x%x in format",
1ccf251f 1801 format_char);
1ccf251f
RK
1802 continue;
1803 }
bb72a084 1804 if (pedantic)
1ccf251f 1805 {
bb72a084
PE
1806 if (index (fci->flag_chars, 'G') != 0)
1807 warning ("ANSI C does not support `%%%c'", format_char);
1808 if (index (fci->flag_chars, 'o') != 0
1809 && index (flag_chars, 'O') != 0)
1810 warning ("ANSI C does not support `%%O%c'", format_char);
1ccf251f 1811 }
bb72a084
PE
1812 if (wide && index (fci->flag_chars, 'w') == 0)
1813 warning ("width used with `%c' format", format_char);
1814 if (index (fci->flag_chars, '2') != 0)
1815 warning ("`%%%c' yields only last 2 digits of year", format_char);
1816 else if (index (fci->flag_chars, '3') != 0)
1817 warning ("`%%%c' yields only last 2 digits of year in some locales",
1818 format_char);
1ccf251f 1819 if (precise && index (fci->flag_chars, 'p') == 0)
bb72a084 1820 warning ("precision used with `%c' format", format_char);
af3c5588
RK
1821 if (aflag && index (fci->flag_chars, 'a') == 0)
1822 {
bb72a084 1823 warning ("`a' flag used with `%c' format", format_char);
f9dcab52
RK
1824 /* To simplify the following code. */
1825 aflag = 0;
1ccf251f 1826 }
c6a9dea8
JW
1827 /* The a flag is a GNU extension. */
1828 else if (pedantic && aflag)
1829 warning ("ANSI C does not support the `a' flag");
bb72a084 1830 if (info->format_type == scanf_format_type && format_char == '[')
1ccf251f
RK
1831 {
1832 /* Skip over scan set, in case it happens to have '%' in it. */
1833 if (*format_chars == '^')
1834 ++format_chars;
1835 /* Find closing bracket; if one is hit immediately, then
1836 it's part of the scan set rather than a terminator. */
1837 if (*format_chars == ']')
1838 ++format_chars;
1839 while (*format_chars && *format_chars != ']')
1840 ++format_chars;
1841 if (*format_chars != ']')
bb72a084
PE
1842 /* The end of the format string was reached. */
1843 warning ("no closing `]' for `%%[' format");
1ccf251f
RK
1844 }
1845 if (suppressed)
1846 {
1847 if (index (fci->flag_chars, '*') == 0)
bb72a084 1848 warning ("suppression of `%c' conversion in format", format_char);
1ccf251f
RK
1849 continue;
1850 }
1851 for (i = 0; flag_chars[i] != 0; ++i)
1852 {
1853 if (index (fci->flag_chars, flag_chars[i]) == 0)
bb72a084
PE
1854 warning ("flag `%c' used with type `%c'",
1855 flag_chars[i], format_char);
1ccf251f 1856 }
bb72a084
PE
1857 if (info->format_type == strftime_format_type)
1858 continue;
f5963e61
JL
1859 if (precise && index (flag_chars, '0') != 0
1860 && (format_char == 'd' || format_char == 'i'
1861 || format_char == 'o' || format_char == 'u'
bf39dde3 1862 || format_char == 'x' || format_char == 'X'))
bb72a084
PE
1863 warning ("`0' flag ignored with precision specifier and `%c' format",
1864 format_char);
1ccf251f
RK
1865 switch (length_char)
1866 {
1867 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
b57062ca 1868 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1ccf251f
RK
1869 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1870 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
2314fb70 1871 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1ccf251f 1872 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
e5e809f4 1873 case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1ccf251f 1874 }
f5963e61 1875 if (wanted_type == 0)
72f54004
JM
1876 warning ("use of `%c' length character with `%c' type character",
1877 length_char, format_char);
1ccf251f 1878
1ccf251f
RK
1879 /* Finally. . .check type of argument against desired type! */
1880 if (info->first_arg_num == 0)
1881 continue;
2a13575e
RK
1882 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1883 /* This specifier takes no argument. */
1884 continue;
1ccf251f
RK
1885 if (params == 0)
1886 {
ab87f8c8 1887 tfaff ();
1ccf251f
RK
1888 return;
1889 }
1890 cur_param = TREE_VALUE (params);
1891 params = TREE_CHAIN (params);
1892 ++arg_num;
1893 cur_type = TREE_TYPE (cur_param);
1894
6d819282
MK
1895 STRIP_NOPS (cur_param);
1896
1ccf251f
RK
1897 /* Check the types of any additional pointer arguments
1898 that precede the "real" argument. */
f9dcab52 1899 for (i = 0; i < fci->pointer_count + aflag; ++i)
1ccf251f
RK
1900 {
1901 if (TREE_CODE (cur_type) == POINTER_TYPE)
1902 {
1903 cur_type = TREE_TYPE (cur_type);
6d819282 1904
f5963e61 1905 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
6d819282
MK
1906 cur_param = TREE_OPERAND (cur_param, 0);
1907 else
1908 cur_param = 0;
1909
1ccf251f
RK
1910 continue;
1911 }
87416640 1912 if (TREE_CODE (cur_type) != ERROR_MARK)
ab87f8c8
JL
1913 warning ((fci->pointer_count + aflag == 1
1914 ? "format argument is not a pointer (arg %d)"
1915 : "format argument is not a pointer to a pointer (arg %d)"),
bb72a084 1916 arg_num);
1ccf251f
RK
1917 break;
1918 }
1919
6d819282 1920 /* See if this is an attempt to write into a const type with
bc516719
AS
1921 scanf or with printf "%n". */
1922 if ((info->format_type == scanf_format_type
1923 || (info->format_type == printf_format_type
1924 && format_char == 'n'))
bb72a084 1925 && i == fci->pointer_count + aflag
6d819282
MK
1926 && wanted_type != 0
1927 && TREE_CODE (cur_type) != ERROR_MARK
1928 && (TYPE_READONLY (cur_type)
1929 || (cur_param != 0
1930 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1931 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1932 && TREE_READONLY (cur_param))))))
bb72a084 1933 warning ("writing into constant object (arg %d)", arg_num);
6d819282 1934
1ccf251f 1935 /* Check the type of the "real" argument, if there's a type we want. */
f9dcab52 1936 if (i == fci->pointer_count + aflag && wanted_type != 0
87416640 1937 && TREE_CODE (cur_type) != ERROR_MARK
1ccf251f
RK
1938 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1939 /* If we want `void *', allow any pointer type.
1940 (Anything else would already have got a warning.) */
1941 && ! (wanted_type == void_type_node
1942 && fci->pointer_count > 0)
1943 /* Don't warn about differences merely in signedness. */
1944 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
b7c9c707 1945 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
4215e498 1946 && (TREE_UNSIGNED (wanted_type)
f5775325
RK
1947 ? wanted_type == (cur_type = unsigned_type (cur_type))
1948 : wanted_type == (cur_type = signed_type (cur_type))))
60e02b1e
RK
1949 /* Likewise, "signed char", "unsigned char" and "char" are
1950 equivalent but the above test won't consider them equivalent. */
1951 && ! (wanted_type == char_type_node
b7c9c707
RS
1952 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1953 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1ccf251f 1954 {
dff01034
KG
1955 register const char *this;
1956 register const char *that;
b57062ca 1957
1ccf251f
RK
1958 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1959 that = 0;
1960 if (TREE_CODE (cur_type) != ERROR_MARK
1961 && TYPE_NAME (cur_type) != 0
1962 && TREE_CODE (cur_type) != INTEGER_TYPE
1963 && !(TREE_CODE (cur_type) == POINTER_TYPE
1964 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1965 {
1966 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1967 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1968 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1969 else
1970 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1971 }
1972
1973 /* A nameless type can't possibly match what the format wants.
1974 So there will be a warning for it.
1975 Make up a string to describe vaguely what it is. */
1976 if (that == 0)
1977 {
1978 if (TREE_CODE (cur_type) == POINTER_TYPE)
1979 that = "pointer";
1980 else
1981 that = "different type";
1982 }
1983
309ffab6
RS
1984 /* Make the warning better in case of mismatch of int vs long. */
1985 if (TREE_CODE (cur_type) == INTEGER_TYPE
1986 && TREE_CODE (wanted_type) == INTEGER_TYPE
1987 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1988 && TYPE_NAME (cur_type) != 0
1989 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1990 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1991
1ccf251f 1992 if (strcmp (this, that) != 0)
bb72a084 1993 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
1ccf251f
RK
1994 }
1995 }
1996}
1997\f
d74154d5
RS
1998/* Print a warning if a constant expression had overflow in folding.
1999 Invoke this function on every expression that the language
2000 requires to be a constant expression.
2001 Note the ANSI C standard says it is erroneous for a
2002 constant expression to overflow. */
96571883
BK
2003
2004void
2005constant_expression_warning (value)
2006 tree value;
2007{
c05f751c
RK
2008 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2009 || TREE_CODE (value) == COMPLEX_CST)
2010 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2011 pedwarn ("overflow in constant expression");
d74154d5
RS
2012}
2013
2014/* Print a warning if an expression had overflow in folding.
2015 Invoke this function on every expression that
2016 (1) appears in the source code, and
2017 (2) might be a constant expression that overflowed, and
2018 (3) is not already checked by convert_and_check;
2019 however, do not invoke this function on operands of explicit casts. */
2020
2021void
2022overflow_warning (value)
2023 tree value;
2024{
c05f751c
RK
2025 if ((TREE_CODE (value) == INTEGER_CST
2026 || (TREE_CODE (value) == COMPLEX_CST
2027 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2028 && TREE_OVERFLOW (value))
d74154d5 2029 {
7193bce2 2030 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
2031 if (skip_evaluation == 0)
2032 warning ("integer overflow in expression");
d74154d5 2033 }
c05f751c
RK
2034 else if ((TREE_CODE (value) == REAL_CST
2035 || (TREE_CODE (value) == COMPLEX_CST
2036 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2037 && TREE_OVERFLOW (value))
2038 {
2039 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
2040 if (skip_evaluation == 0)
2041 warning ("floating point overflow in expression");
c05f751c 2042 }
d74154d5
RS
2043}
2044
2045/* Print a warning if a large constant is truncated to unsigned,
2046 or if -Wconversion is used and a constant < 0 is converted to unsigned.
2047 Invoke this function on every expression that might be implicitly
2048 converted to an unsigned type. */
2049
2050void
2051unsigned_conversion_warning (result, operand)
2052 tree result, operand;
2053{
2054 if (TREE_CODE (operand) == INTEGER_CST
2055 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2056 && TREE_UNSIGNED (TREE_TYPE (result))
e78a3b42 2057 && skip_evaluation == 0
d74154d5
RS
2058 && !int_fits_type_p (operand, TREE_TYPE (result)))
2059 {
2060 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2061 /* This detects cases like converting -129 or 256 to unsigned char. */
90c939d4 2062 warning ("large integer implicitly truncated to unsigned type");
d74154d5 2063 else if (warn_conversion)
90c939d4 2064 warning ("negative integer implicitly converted to unsigned type");
d74154d5
RS
2065 }
2066}
2067
2068/* Convert EXPR to TYPE, warning about conversion problems with constants.
2069 Invoke this function on every expression that is converted implicitly,
2070 i.e. because of language rules and not because of an explicit cast. */
2071
2072tree
2073convert_and_check (type, expr)
2074 tree type, expr;
2075{
2076 tree t = convert (type, expr);
2077 if (TREE_CODE (t) == INTEGER_CST)
2078 {
7193bce2 2079 if (TREE_OVERFLOW (t))
d74154d5 2080 {
7193bce2
PE
2081 TREE_OVERFLOW (t) = 0;
2082
868fc750
RK
2083 /* Do not diagnose overflow in a constant expression merely
2084 because a conversion overflowed. */
2085 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2086
7193bce2
PE
2087 /* No warning for converting 0x80000000 to int. */
2088 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2089 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2090 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
22ba338b
RS
2091 /* If EXPR fits in the unsigned version of TYPE,
2092 don't warn unless pedantic. */
e78a3b42
RK
2093 if ((pedantic
2094 || TREE_UNSIGNED (type)
2095 || ! int_fits_type_p (expr, unsigned_type (type)))
2096 && skip_evaluation == 0)
bb72a084 2097 warning ("overflow in implicit constant conversion");
d74154d5
RS
2098 }
2099 else
2100 unsigned_conversion_warning (t, expr);
2101 }
2102 return t;
96571883
BK
2103}
2104\f
b30f223b
RS
2105void
2106c_expand_expr_stmt (expr)
2107 tree expr;
2108{
2109 /* Do default conversion if safe and possibly important,
2110 in case within ({...}). */
2111 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2112 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2113 expr = default_conversion (expr);
2114
2115 if (TREE_TYPE (expr) != error_mark_node
2116 && TYPE_SIZE (TREE_TYPE (expr)) == 0
2117 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2118 error ("expression statement has incomplete type");
2119
2120 expand_expr_stmt (expr);
2121}
2122\f
2123/* Validate the expression after `case' and apply default promotions. */
2124
2125tree
2126check_case_value (value)
2127 tree value;
2128{
2129 if (value == NULL_TREE)
2130 return value;
2131
2132 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8493738b 2133 STRIP_TYPE_NOPS (value);
b30f223b
RS
2134
2135 if (TREE_CODE (value) != INTEGER_CST
2136 && value != error_mark_node)
2137 {
2138 error ("case label does not reduce to an integer constant");
2139 value = error_mark_node;
2140 }
2141 else
2142 /* Promote char or short to int. */
2143 value = default_conversion (value);
2144
bc690db1
RS
2145 constant_expression_warning (value);
2146
b30f223b
RS
2147 return value;
2148}
2149\f
2150/* Return an integer type with BITS bits of precision,
2151 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2152
2153tree
2154type_for_size (bits, unsignedp)
2155 unsigned bits;
2156 int unsignedp;
2157{
a311b52c
JM
2158 if (bits == TYPE_PRECISION (integer_type_node))
2159 return unsignedp ? unsigned_type_node : integer_type_node;
2160
3fc7e390 2161 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
2162 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2163
3fc7e390 2164 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
2165 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2166
3fc7e390 2167 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
2168 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2169
3fc7e390 2170 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
2171 return (unsignedp ? long_long_unsigned_type_node
2172 : long_long_integer_type_node);
2173
835f9b4d
GRK
2174 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2175 return (unsignedp ? widest_unsigned_literal_type_node
2176 : widest_integer_literal_type_node);
2177
3fc7e390
RS
2178 if (bits <= TYPE_PRECISION (intQI_type_node))
2179 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2180
2181 if (bits <= TYPE_PRECISION (intHI_type_node))
2182 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2183
2184 if (bits <= TYPE_PRECISION (intSI_type_node))
2185 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2186
2187 if (bits <= TYPE_PRECISION (intDI_type_node))
2188 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2189
b30f223b
RS
2190 return 0;
2191}
2192
2193/* Return a data type that has machine mode MODE.
2194 If the mode is an integer,
2195 then UNSIGNEDP selects between signed and unsigned types. */
2196
2197tree
2198type_for_mode (mode, unsignedp)
2199 enum machine_mode mode;
2200 int unsignedp;
2201{
a311b52c
JM
2202 if (mode == TYPE_MODE (integer_type_node))
2203 return unsignedp ? unsigned_type_node : integer_type_node;
2204
b30f223b
RS
2205 if (mode == TYPE_MODE (signed_char_type_node))
2206 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2207
2208 if (mode == TYPE_MODE (short_integer_type_node))
2209 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2210
b30f223b
RS
2211 if (mode == TYPE_MODE (long_integer_type_node))
2212 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2213
2214 if (mode == TYPE_MODE (long_long_integer_type_node))
2215 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2216
835f9b4d
GRK
2217 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2218 return unsignedp ? widest_unsigned_literal_type_node
2219 : widest_integer_literal_type_node;
2220
3fc7e390
RS
2221 if (mode == TYPE_MODE (intQI_type_node))
2222 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2223
2224 if (mode == TYPE_MODE (intHI_type_node))
2225 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2226
2227 if (mode == TYPE_MODE (intSI_type_node))
2228 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2229
2230 if (mode == TYPE_MODE (intDI_type_node))
2231 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2232
21a9616b 2233#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
2234 if (mode == TYPE_MODE (intTI_type_node))
2235 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 2236#endif
a6d7e156 2237
b30f223b
RS
2238 if (mode == TYPE_MODE (float_type_node))
2239 return float_type_node;
2240
2241 if (mode == TYPE_MODE (double_type_node))
2242 return double_type_node;
2243
2244 if (mode == TYPE_MODE (long_double_type_node))
2245 return long_double_type_node;
2246
2247 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2248 return build_pointer_type (char_type_node);
2249
2250 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2251 return build_pointer_type (integer_type_node);
2252
2253 return 0;
2254}
693a6128
GRK
2255
2256/* Return an unsigned type the same as TYPE in other respects. */
2257tree
2258unsigned_type (type)
2259 tree type;
2260{
2261 tree type1 = TYPE_MAIN_VARIANT (type);
2262 if (type1 == signed_char_type_node || type1 == char_type_node)
2263 return unsigned_char_type_node;
2264 if (type1 == integer_type_node)
2265 return unsigned_type_node;
2266 if (type1 == short_integer_type_node)
2267 return short_unsigned_type_node;
2268 if (type1 == long_integer_type_node)
2269 return long_unsigned_type_node;
2270 if (type1 == long_long_integer_type_node)
2271 return long_long_unsigned_type_node;
2272 if (type1 == widest_integer_literal_type_node)
2273 return widest_unsigned_literal_type_node;
2274#if HOST_BITS_PER_WIDE_INT >= 64
2275 if (type1 == intTI_type_node)
2276 return unsigned_intTI_type_node;
2277#endif
2278 if (type1 == intDI_type_node)
2279 return unsigned_intDI_type_node;
2280 if (type1 == intSI_type_node)
2281 return unsigned_intSI_type_node;
2282 if (type1 == intHI_type_node)
2283 return unsigned_intHI_type_node;
2284 if (type1 == intQI_type_node)
2285 return unsigned_intQI_type_node;
2286
2287 return signed_or_unsigned_type (1, type);
2288}
2289
2290/* Return a signed type the same as TYPE in other respects. */
2291
2292tree
2293signed_type (type)
2294 tree type;
2295{
2296 tree type1 = TYPE_MAIN_VARIANT (type);
2297 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2298 return signed_char_type_node;
2299 if (type1 == unsigned_type_node)
2300 return integer_type_node;
2301 if (type1 == short_unsigned_type_node)
2302 return short_integer_type_node;
2303 if (type1 == long_unsigned_type_node)
2304 return long_integer_type_node;
2305 if (type1 == long_long_unsigned_type_node)
2306 return long_long_integer_type_node;
2307 if (type1 == widest_unsigned_literal_type_node)
2308 return widest_integer_literal_type_node;
2309#if HOST_BITS_PER_WIDE_INT >= 64
2310 if (type1 == unsigned_intTI_type_node)
2311 return intTI_type_node;
2312#endif
2313 if (type1 == unsigned_intDI_type_node)
2314 return intDI_type_node;
2315 if (type1 == unsigned_intSI_type_node)
2316 return intSI_type_node;
2317 if (type1 == unsigned_intHI_type_node)
2318 return intHI_type_node;
2319 if (type1 == unsigned_intQI_type_node)
2320 return intQI_type_node;
2321
2322 return signed_or_unsigned_type (0, type);
2323}
2324
2325/* Return a type the same as TYPE except unsigned or
2326 signed according to UNSIGNEDP. */
2327
2328tree
2329signed_or_unsigned_type (unsignedp, type)
2330 int unsignedp;
2331 tree type;
2332{
2333 if (! INTEGRAL_TYPE_P (type)
2334 || TREE_UNSIGNED (type) == unsignedp)
2335 return type;
2336
2337 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2338 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2339 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2340 return unsignedp ? unsigned_type_node : integer_type_node;
2341 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
2342 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2343 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
2344 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2345 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
2346 return (unsignedp ? long_long_unsigned_type_node
2347 : long_long_integer_type_node);
2348 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
2349 return (unsignedp ? widest_unsigned_literal_type_node
2350 : widest_integer_literal_type_node);
2351 return type;
2352}
b30f223b 2353\f
6acfe908
JM
2354/* Return the minimum number of bits needed to represent VALUE in a
2355 signed or unsigned type, UNSIGNEDP says which. */
2356
2357int
2358min_precision (value, unsignedp)
2359 tree value;
2360 int unsignedp;
2361{
2362 int log;
2363
2364 /* If the value is negative, compute its negative minus 1. The latter
2365 adjustment is because the absolute value of the largest negative value
2366 is one larger than the largest positive value. This is equivalent to
2367 a bit-wise negation, so use that operation instead. */
2368
2369 if (tree_int_cst_sgn (value) < 0)
2370 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2371
2372 /* Return the number of bits needed, taking into account the fact
2373 that we need one more bit for a signed than unsigned type. */
2374
2375 if (integer_zerop (value))
2376 log = 0;
2377 else if (TREE_INT_CST_HIGH (value) != 0)
2378 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2379 else
2380 log = floor_log2 (TREE_INT_CST_LOW (value));
2381
2382 return log + 1 + ! unsignedp;
2383}
2384\f
b30f223b
RS
2385/* Print an error message for invalid operands to arith operation CODE.
2386 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2387
2388void
2389binary_op_error (code)
2390 enum tree_code code;
2391{
dff01034 2392 register const char *opname;
89c78d7d 2393
b30f223b
RS
2394 switch (code)
2395 {
2396 case NOP_EXPR:
2397 error ("invalid truth-value expression");
2398 return;
2399
2400 case PLUS_EXPR:
2401 opname = "+"; break;
2402 case MINUS_EXPR:
2403 opname = "-"; break;
2404 case MULT_EXPR:
2405 opname = "*"; break;
2406 case MAX_EXPR:
2407 opname = "max"; break;
2408 case MIN_EXPR:
2409 opname = "min"; break;
2410 case EQ_EXPR:
2411 opname = "=="; break;
2412 case NE_EXPR:
2413 opname = "!="; break;
2414 case LE_EXPR:
2415 opname = "<="; break;
2416 case GE_EXPR:
2417 opname = ">="; break;
2418 case LT_EXPR:
2419 opname = "<"; break;
2420 case GT_EXPR:
2421 opname = ">"; break;
2422 case LSHIFT_EXPR:
2423 opname = "<<"; break;
2424 case RSHIFT_EXPR:
2425 opname = ">>"; break;
2426 case TRUNC_MOD_EXPR:
047de90b 2427 case FLOOR_MOD_EXPR:
b30f223b
RS
2428 opname = "%"; break;
2429 case TRUNC_DIV_EXPR:
047de90b 2430 case FLOOR_DIV_EXPR:
b30f223b
RS
2431 opname = "/"; break;
2432 case BIT_AND_EXPR:
2433 opname = "&"; break;
2434 case BIT_IOR_EXPR:
2435 opname = "|"; break;
2436 case TRUTH_ANDIF_EXPR:
2437 opname = "&&"; break;
2438 case TRUTH_ORIF_EXPR:
2439 opname = "||"; break;
2440 case BIT_XOR_EXPR:
2441 opname = "^"; break;
047de90b
RS
2442 case LROTATE_EXPR:
2443 case RROTATE_EXPR:
2444 opname = "rotate"; break;
6d819282
MK
2445 default:
2446 opname = "unknown"; break;
b30f223b
RS
2447 }
2448 error ("invalid operands to binary %s", opname);
2449}
2450\f
2451/* Subroutine of build_binary_op, used for comparison operations.
2452 See if the operands have both been converted from subword integer types
2453 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
2454 This function is also responsible for converting the two operands
2455 to the proper common type for comparison.
b30f223b
RS
2456
2457 The arguments of this function are all pointers to local variables
2458 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2459 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2460
2461 If this function returns nonzero, it means that the comparison has
2462 a constant value. What this function returns is an expression for
2463 that value. */
2464
2465tree
2466shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2467 tree *op0_ptr, *op1_ptr;
2468 tree *restype_ptr;
2469 enum tree_code *rescode_ptr;
2470{
2471 register tree type;
2472 tree op0 = *op0_ptr;
2473 tree op1 = *op1_ptr;
2474 int unsignedp0, unsignedp1;
2475 int real1, real2;
2476 tree primop0, primop1;
2477 enum tree_code code = *rescode_ptr;
2478
2479 /* Throw away any conversions to wider types
2480 already present in the operands. */
2481
2482 primop0 = get_narrower (op0, &unsignedp0);
2483 primop1 = get_narrower (op1, &unsignedp1);
2484
2485 /* Handle the case that OP0 does not *contain* a conversion
2486 but it *requires* conversion to FINAL_TYPE. */
2487
2488 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2489 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2490 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2491 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2492
2493 /* If one of the operands must be floated, we cannot optimize. */
2494 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2495 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2496
2497 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
2498 so value is preserved), for canonicalization. Don't do this if
2499 the second arg is 0. */
b30f223b 2500
5af6001b
RK
2501 if (TREE_CONSTANT (primop0)
2502 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b30f223b
RS
2503 {
2504 register tree tem = primop0;
2505 register int temi = unsignedp0;
2506 primop0 = primop1;
2507 primop1 = tem;
2508 tem = op0;
2509 op0 = op1;
2510 op1 = tem;
2511 *op0_ptr = op0;
2512 *op1_ptr = op1;
2513 unsignedp0 = unsignedp1;
2514 unsignedp1 = temi;
2515 temi = real1;
2516 real1 = real2;
2517 real2 = temi;
2518
2519 switch (code)
2520 {
2521 case LT_EXPR:
2522 code = GT_EXPR;
2523 break;
2524 case GT_EXPR:
2525 code = LT_EXPR;
2526 break;
2527 case LE_EXPR:
2528 code = GE_EXPR;
2529 break;
2530 case GE_EXPR:
2531 code = LE_EXPR;
2532 break;
6d819282
MK
2533 default:
2534 break;
b30f223b
RS
2535 }
2536 *rescode_ptr = code;
2537 }
2538
2539 /* If comparing an integer against a constant more bits wide,
2540 maybe we can deduce a value of 1 or 0 independent of the data.
2541 Or else truncate the constant now
2542 rather than extend the variable at run time.
2543
2544 This is only interesting if the constant is the wider arg.
2545 Also, it is not safe if the constant is unsigned and the
2546 variable arg is signed, since in this case the variable
2547 would be sign-extended and then regarded as unsigned.
2548 Our technique fails in this case because the lowest/highest
2549 possible unsigned results don't follow naturally from the
2550 lowest/highest possible values of the variable operand.
2551 For just EQ_EXPR and NE_EXPR there is another technique that
2552 could be used: see if the constant can be faithfully represented
2553 in the other operand's type, by truncating it and reextending it
2554 and see if that preserves the constant's value. */
2555
2556 if (!real1 && !real2
2557 && TREE_CODE (primop1) == INTEGER_CST
2558 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2559 {
2560 int min_gt, max_gt, min_lt, max_lt;
2561 tree maxval, minval;
2562 /* 1 if comparison is nominally unsigned. */
2563 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2564 tree val;
2565
2566 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
8bbd5685
CW
2567
2568 /* If TYPE is an enumeration, then we need to get its min/max
2569 values from it's underlying integral type, not the enumerated
2570 type itself. */
2571 if (TREE_CODE (type) == ENUMERAL_TYPE)
2572 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
b30f223b
RS
2573
2574 maxval = TYPE_MAX_VALUE (type);
2575 minval = TYPE_MIN_VALUE (type);
2576
2577 if (unsignedp && !unsignedp0)
2578 *restype_ptr = signed_type (*restype_ptr);
2579
2580 if (TREE_TYPE (primop1) != *restype_ptr)
2581 primop1 = convert (*restype_ptr, primop1);
2582 if (type != *restype_ptr)
2583 {
2584 minval = convert (*restype_ptr, minval);
2585 maxval = convert (*restype_ptr, maxval);
2586 }
2587
2588 if (unsignedp && unsignedp0)
2589 {
2590 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2591 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2592 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2593 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2594 }
2595 else
2596 {
2597 min_gt = INT_CST_LT (primop1, minval);
2598 max_gt = INT_CST_LT (primop1, maxval);
2599 min_lt = INT_CST_LT (minval, primop1);
2600 max_lt = INT_CST_LT (maxval, primop1);
2601 }
2602
2603 val = 0;
2604 /* This used to be a switch, but Genix compiler can't handle that. */
2605 if (code == NE_EXPR)
2606 {
2607 if (max_lt || min_gt)
a360da3a 2608 val = boolean_true_node;
b30f223b
RS
2609 }
2610 else if (code == EQ_EXPR)
2611 {
2612 if (max_lt || min_gt)
a360da3a 2613 val = boolean_false_node;
b30f223b
RS
2614 }
2615 else if (code == LT_EXPR)
2616 {
2617 if (max_lt)
a360da3a 2618 val = boolean_true_node;
b30f223b 2619 if (!min_lt)
a360da3a 2620 val = boolean_false_node;
b30f223b
RS
2621 }
2622 else if (code == GT_EXPR)
2623 {
2624 if (min_gt)
a360da3a 2625 val = boolean_true_node;
b30f223b 2626 if (!max_gt)
a360da3a 2627 val = boolean_false_node;
b30f223b
RS
2628 }
2629 else if (code == LE_EXPR)
2630 {
2631 if (!max_gt)
a360da3a 2632 val = boolean_true_node;
b30f223b 2633 if (min_gt)
a360da3a 2634 val = boolean_false_node;
b30f223b
RS
2635 }
2636 else if (code == GE_EXPR)
2637 {
2638 if (!min_lt)
a360da3a 2639 val = boolean_true_node;
b30f223b 2640 if (max_lt)
a360da3a 2641 val = boolean_false_node;
b30f223b
RS
2642 }
2643
2644 /* If primop0 was sign-extended and unsigned comparison specd,
2645 we did a signed comparison above using the signed type bounds.
2646 But the comparison we output must be unsigned.
2647
2648 Also, for inequalities, VAL is no good; but if the signed
2649 comparison had *any* fixed result, it follows that the
2650 unsigned comparison just tests the sign in reverse
2651 (positive values are LE, negative ones GE).
2652 So we can generate an unsigned comparison
2653 against an extreme value of the signed type. */
2654
2655 if (unsignedp && !unsignedp0)
2656 {
2657 if (val != 0)
2658 switch (code)
2659 {
2660 case LT_EXPR:
2661 case GE_EXPR:
2662 primop1 = TYPE_MIN_VALUE (type);
2663 val = 0;
2664 break;
2665
2666 case LE_EXPR:
2667 case GT_EXPR:
2668 primop1 = TYPE_MAX_VALUE (type);
2669 val = 0;
2670 break;
6d819282
MK
2671
2672 default:
2673 break;
b30f223b
RS
2674 }
2675 type = unsigned_type (type);
2676 }
2677
b7c9c707 2678 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b
RS
2679 {
2680 /* This is the case of (char)x >?< 0x80, which people used to use
2681 expecting old C compilers to change the 0x80 into -0x80. */
a360da3a 2682 if (val == boolean_false_node)
07be2a23 2683 warning ("comparison is always false due to limited range of data type");
a360da3a 2684 if (val == boolean_true_node)
07be2a23 2685 warning ("comparison is always true due to limited range of data type");
b30f223b
RS
2686 }
2687
b7c9c707 2688 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b 2689 {
1e276c4a 2690 /* This is the case of (unsigned char)x >?< -1 or < 0. */
a360da3a 2691 if (val == boolean_false_node)
07be2a23 2692 warning ("comparison is always false due to limited range of data type");
a360da3a 2693 if (val == boolean_true_node)
07be2a23 2694 warning ("comparison is always true due to limited range of data type");
b30f223b
RS
2695 }
2696
2697 if (val != 0)
2698 {
2699 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2700 if (TREE_SIDE_EFFECTS (primop0))
2701 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2702 return val;
2703 }
2704
2705 /* Value is not predetermined, but do the comparison
2706 in the type of the operand that is not constant.
2707 TYPE is already properly set. */
2708 }
2709 else if (real1 && real2
766f6c30
RS
2710 && (TYPE_PRECISION (TREE_TYPE (primop0))
2711 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
2712 type = TREE_TYPE (primop0);
2713
2714 /* If args' natural types are both narrower than nominal type
2715 and both extend in the same manner, compare them
2716 in the type of the wider arg.
2717 Otherwise must actually extend both to the nominal
2718 common type lest different ways of extending
2719 alter the result.
2720 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2721
2722 else if (unsignedp0 == unsignedp1 && real1 == real2
2723 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2724 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2725 {
2726 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2727 type = signed_or_unsigned_type (unsignedp0
2728 || TREE_UNSIGNED (*restype_ptr),
2729 type);
2730 /* Make sure shorter operand is extended the right way
2731 to match the longer operand. */
2732 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2733 primop0);
2734 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2735 primop1);
2736 }
2737 else
2738 {
2739 /* Here we must do the comparison on the nominal type
2740 using the args exactly as we received them. */
2741 type = *restype_ptr;
2742 primop0 = op0;
2743 primop1 = op1;
2744
2745 if (!real1 && !real2 && integer_zerop (primop1)
597681f6 2746 && TREE_UNSIGNED (*restype_ptr))
b30f223b
RS
2747 {
2748 tree value = 0;
2749 switch (code)
2750 {
2751 case GE_EXPR:
5af6001b
RK
2752 /* All unsigned values are >= 0, so we warn if extra warnings
2753 are requested. However, if OP0 is a constant that is
2754 >= 0, the signedness of the comparison isn't an issue,
2755 so suppress the warning. */
2756 if (extra_warnings
2757 && ! (TREE_CODE (primop0) == INTEGER_CST
2758 && ! TREE_OVERFLOW (convert (signed_type (type),
2759 primop0))))
07be2a23 2760 warning ("comparison of unsigned expression >= 0 is always true");
a360da3a 2761 value = boolean_true_node;
b30f223b
RS
2762 break;
2763
2764 case LT_EXPR:
5af6001b
RK
2765 if (extra_warnings
2766 && ! (TREE_CODE (primop0) == INTEGER_CST
2767 && ! TREE_OVERFLOW (convert (signed_type (type),
2768 primop0))))
07be2a23 2769 warning ("comparison of unsigned expression < 0 is always false");
a360da3a 2770 value = boolean_false_node;
6d819282
MK
2771 break;
2772
2773 default:
2774 break;
b30f223b
RS
2775 }
2776
2777 if (value != 0)
2778 {
2779 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2780 if (TREE_SIDE_EFFECTS (primop0))
2781 return build (COMPOUND_EXPR, TREE_TYPE (value),
2782 primop0, value);
2783 return value;
2784 }
2785 }
2786 }
2787
2788 *op0_ptr = convert (type, primop0);
2789 *op1_ptr = convert (type, primop1);
2790
a360da3a 2791 *restype_ptr = boolean_type_node;
b30f223b
RS
2792
2793 return 0;
2794}
2795\f
2796/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2797 or validate its data type for an `if' or `while' statement or ?..: exp.
2798
2799 This preparation consists of taking the ordinary
2800 representation of an expression expr and producing a valid tree
2801 boolean expression describing whether expr is nonzero. We could
a360da3a 2802 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b30f223b
RS
2803 but we optimize comparisons, &&, ||, and !.
2804
a360da3a 2805 The resulting type should always be `boolean_type_node'. */
b30f223b
RS
2806
2807tree
2808truthvalue_conversion (expr)
2809 tree expr;
2810{
257e61ed
RS
2811 if (TREE_CODE (expr) == ERROR_MARK)
2812 return expr;
2813
d7c83727 2814#if 0 /* This appears to be wrong for C++. */
257e61ed
RS
2815 /* These really should return error_mark_node after 2.4 is stable.
2816 But not all callers handle ERROR_MARK properly. */
2817 switch (TREE_CODE (TREE_TYPE (expr)))
2818 {
2819 case RECORD_TYPE:
2820 error ("struct type value used where scalar is required");
a360da3a 2821 return boolean_false_node;
257e61ed
RS
2822
2823 case UNION_TYPE:
2824 error ("union type value used where scalar is required");
a360da3a 2825 return boolean_false_node;
257e61ed
RS
2826
2827 case ARRAY_TYPE:
2828 error ("array type value used where scalar is required");
a360da3a 2829 return boolean_false_node;
257e61ed
RS
2830
2831 default:
2832 break;
2833 }
d7c83727 2834#endif /* 0 */
257e61ed 2835
b30f223b
RS
2836 switch (TREE_CODE (expr))
2837 {
2838 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2839 or comparison expressions as truth values at this level. */
2840#if 0
2841 case COMPONENT_REF:
2842 /* A one-bit unsigned bit-field is already acceptable. */
2843 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2844 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2845 return expr;
2846 break;
2847#endif
2848
2849 case EQ_EXPR:
2850 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2851 or comparison expressions as truth values at this level. */
2852#if 0
2853 if (integer_zerop (TREE_OPERAND (expr, 1)))
2854 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2855#endif
2856 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2857 case TRUTH_ANDIF_EXPR:
2858 case TRUTH_ORIF_EXPR:
2859 case TRUTH_AND_EXPR:
2860 case TRUTH_OR_EXPR:
9379fac9 2861 case TRUTH_XOR_EXPR:
1180eb10 2862 case TRUTH_NOT_EXPR:
a360da3a
JM
2863 TREE_TYPE (expr) = boolean_type_node;
2864 return expr;
18c0f675 2865
b30f223b
RS
2866 case ERROR_MARK:
2867 return expr;
2868
2869 case INTEGER_CST:
a360da3a 2870 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2871
2872 case REAL_CST:
a360da3a 2873 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2874
2875 case ADDR_EXPR:
fc0c675f
RK
2876 /* If we are taking the address of a external decl, it might be zero
2877 if it is weak, so we cannot optimize. */
2878 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2879 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2880 break;
2881
b30f223b 2882 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
a360da3a
JM
2883 return build (COMPOUND_EXPR, boolean_type_node,
2884 TREE_OPERAND (expr, 0), boolean_true_node);
b30f223b 2885 else
a360da3a 2886 return boolean_true_node;
b30f223b 2887
766f6c30 2888 case COMPLEX_EXPR:
f0b996c5 2889 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 2890 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
2891 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2892 truthvalue_conversion (TREE_OPERAND (expr, 1)),
766f6c30
RS
2893 0);
2894
b30f223b
RS
2895 case NEGATE_EXPR:
2896 case ABS_EXPR:
2897 case FLOAT_EXPR:
2898 case FFS_EXPR:
2899 /* These don't change whether an object is non-zero or zero. */
2900 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2901
2902 case LROTATE_EXPR:
2903 case RROTATE_EXPR:
2904 /* These don't change whether an object is zero or non-zero, but
2905 we can't ignore them if their second arg has side-effects. */
2906 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
a360da3a 2907 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
b30f223b
RS
2908 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2909 else
2910 return truthvalue_conversion (TREE_OPERAND (expr, 0));
b57062ca 2911
b30f223b
RS
2912 case COND_EXPR:
2913 /* Distribute the conversion into the arms of a COND_EXPR. */
a360da3a 2914 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
b30f223b
RS
2915 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2916 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2917
2918 case CONVERT_EXPR:
2919 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2920 since that affects how `default_conversion' will behave. */
2921 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2922 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2923 break;
0f41302f 2924 /* fall through... */
b30f223b
RS
2925 case NOP_EXPR:
2926 /* If this is widening the argument, we can ignore it. */
2927 if (TYPE_PRECISION (TREE_TYPE (expr))
2928 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2929 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2930 break;
2931
b30f223b 2932 case MINUS_EXPR:
f87550e0
JW
2933 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2934 this case. */
2935 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2936 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2937 break;
0f41302f 2938 /* fall through... */
f87550e0 2939 case BIT_XOR_EXPR:
d7c83727 2940 /* This and MINUS_EXPR can be changed into a comparison of the
f87550e0 2941 two objects. */
b30f223b
RS
2942 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2943 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2944 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2945 TREE_OPERAND (expr, 1), 1);
2946 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2947 fold (build1 (NOP_EXPR,
2948 TREE_TYPE (TREE_OPERAND (expr, 0)),
2949 TREE_OPERAND (expr, 1))), 1);
e2aab13d 2950
fb48b1f0 2951 case BIT_AND_EXPR:
58cee643
RK
2952 if (integer_onep (TREE_OPERAND (expr, 1))
2953 && TREE_TYPE (expr) != boolean_type_node)
2954 /* Using convert here would cause infinite recursion. */
2955 return build1 (NOP_EXPR, boolean_type_node, expr);
2956 break;
fb48b1f0 2957
e2aab13d
RS
2958 case MODIFY_EXPR:
2959 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2960 warning ("suggest parentheses around assignment used as truth value");
2961 break;
b57062ca 2962
6d819282
MK
2963 default:
2964 break;
b30f223b
RS
2965 }
2966
f0b996c5 2967 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa
AS
2968 {
2969 tree tem = save_expr (expr);
2970 return (build_binary_op
2971 ((TREE_SIDE_EFFECTS (expr)
2972 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2973 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2974 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2975 0));
2976 }
f0b996c5 2977
b30f223b
RS
2978 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2979}
2980\f
c8724862
DB
2981#if USE_CPPLIB
2982/* Read the rest of a #-directive from input stream FINPUT.
2983 In normal use, the directive name and the white space after it
2984 have already been read, so they won't be included in the result.
2985 We allow for the fact that the directive line may contain
2986 a newline embedded within a character or string literal which forms
2987 a part of the directive.
2988
2989 The value is a string in a reusable buffer. It remains valid
2990 only until the next time this function is called. */
2991unsigned char *yy_cur, *yy_lim;
2992
2993#define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
4d9a1b48 2994#define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
c8724862
DB
2995
2996int
2997yy_get_token ()
2998{
2999 for (;;)
3000 {
3001 parse_in.limit = parse_in.token_buffer;
3002 cpp_token = cpp_get_token (&parse_in);
3003 if (cpp_token == CPP_EOF)
3004 return -1;
3005 yy_lim = CPP_PWRITTEN (&parse_in);
3006 yy_cur = parse_in.token_buffer;
3007 if (yy_cur < yy_lim)
3008 return *yy_cur++;
3009 }
3010}
3011
3012char *
3013get_directive_line ()
3014{
3015 static char *directive_buffer = NULL;
3016 static unsigned buffer_length = 0;
3017 register char *p;
3018 register char *buffer_limit;
3019 register int looking_for = 0;
3020 register int char_escaped = 0;
3021
3022 if (buffer_length == 0)
3023 {
3024 directive_buffer = (char *)xmalloc (128);
3025 buffer_length = 128;
3026 }
3027
3028 buffer_limit = &directive_buffer[buffer_length];
3029
3030 for (p = directive_buffer; ; )
3031 {
3032 int c;
3033
3034 /* Make buffer bigger if it is full. */
3035 if (p >= buffer_limit)
3036 {
3037 register unsigned bytes_used = (p - directive_buffer);
3038
3039 buffer_length *= 2;
3040 directive_buffer
3041 = (char *)xrealloc (directive_buffer, buffer_length);
3042 p = &directive_buffer[bytes_used];
3043 buffer_limit = &directive_buffer[buffer_length];
3044 }
3045
3046 c = GETC ();
3047
3048 /* Discard initial whitespace. */
3049 if ((c == ' ' || c == '\t') && p == directive_buffer)
3050 continue;
3051
3052 /* Detect the end of the directive. */
3053 if (c == '\n' && looking_for == 0)
3054 {
3055 UNGETC (c);
3056 c = '\0';
3057 }
3058
3059 *p++ = c;
3060
3061 if (c == 0)
3062 return directive_buffer;
3063
3064 /* Handle string and character constant syntax. */
3065 if (looking_for)
3066 {
3067 if (looking_for == c && !char_escaped)
3068 looking_for = 0; /* Found terminator... stop looking. */
3069 }
3070 else
3071 if (c == '\'' || c == '"')
3072 looking_for = c; /* Don't stop buffering until we see another
3073 another one of these (or an EOF). */
3074
3075 /* Handle backslash. */
3076 char_escaped = (c == '\\' && ! char_escaped);
3077 }
3078}
3079#else
b30f223b
RS
3080/* Read the rest of a #-directive from input stream FINPUT.
3081 In normal use, the directive name and the white space after it
3082 have already been read, so they won't be included in the result.
3083 We allow for the fact that the directive line may contain
3084 a newline embedded within a character or string literal which forms
3085 a part of the directive.
3086
3087 The value is a string in a reusable buffer. It remains valid
05a81fe5
DE
3088 only until the next time this function is called.
3089
3090 The terminating character ('\n' or EOF) is left in FINPUT for the
3091 caller to re-read. */
b30f223b
RS
3092
3093char *
3094get_directive_line (finput)
3095 register FILE *finput;
3096{
3097 static char *directive_buffer = NULL;
3098 static unsigned buffer_length = 0;
3099 register char *p;
3100 register char *buffer_limit;
3101 register int looking_for = 0;
3102 register int char_escaped = 0;
3103
3104 if (buffer_length == 0)
3105 {
3106 directive_buffer = (char *)xmalloc (128);
3107 buffer_length = 128;
3108 }
3109
3110 buffer_limit = &directive_buffer[buffer_length];
3111
3112 for (p = directive_buffer; ; )
3113 {
3114 int c;
3115
3116 /* Make buffer bigger if it is full. */
3117 if (p >= buffer_limit)
3118 {
3119 register unsigned bytes_used = (p - directive_buffer);
3120
3121 buffer_length *= 2;
3122 directive_buffer
3123 = (char *)xrealloc (directive_buffer, buffer_length);
3124 p = &directive_buffer[bytes_used];
3125 buffer_limit = &directive_buffer[buffer_length];
3126 }
3127
3128 c = getc (finput);
3129
3130 /* Discard initial whitespace. */
3131 if ((c == ' ' || c == '\t') && p == directive_buffer)
3132 continue;
3133
3134 /* Detect the end of the directive. */
05a81fe5
DE
3135 if (looking_for == 0
3136 && (c == '\n' || c == EOF))
b30f223b
RS
3137 {
3138 ungetc (c, finput);
3139 c = '\0';
3140 }
3141
3142 *p++ = c;
3143
3144 if (c == 0)
3145 return directive_buffer;
3146
3147 /* Handle string and character constant syntax. */
3148 if (looking_for)
3149 {
3150 if (looking_for == c && !char_escaped)
3151 looking_for = 0; /* Found terminator... stop looking. */
3152 }
3153 else
3154 if (c == '\'' || c == '"')
3155 looking_for = c; /* Don't stop buffering until we see another
38e01259 3156 one of these (or an EOF). */
b30f223b
RS
3157
3158 /* Handle backslash. */
3159 char_escaped = (c == '\\' && ! char_escaped);
3160 }
3161}
c8724862 3162#endif /* !USE_CPPLIB */
0b73773c
NH
3163\f
3164/* Make a variant type in the proper way for C/C++, propagating qualifiers
3165 down to the element type of an array. */
3166
3167tree
3932261a 3168c_build_qualified_type (type, type_quals)
0b73773c 3169 tree type;
3932261a 3170 int type_quals;
0b73773c 3171{
3932261a
MM
3172 /* A restrict-qualified pointer type must be a pointer to object or
3173 incomplete type. Note that the use of POINTER_TYPE_P also allows
3174 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3175 the C++ front-end also use POINTER_TYPE for pointer-to-member
3176 values, so even though it should be illegal to use `restrict'
3177 with such an entity we don't flag that here. Thus, special case
3178 code for that case is required in the C++ front-end. */
3179 if ((type_quals & TYPE_QUAL_RESTRICT)
3180 && (!POINTER_TYPE_P (type)
3181 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3182 {
3183 error ("invalid use of `restrict'");
3184 type_quals &= ~TYPE_QUAL_RESTRICT;
3185 }
3186
0b73773c 3187 if (TREE_CODE (type) == ARRAY_TYPE)
3932261a
MM
3188 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3189 type_quals),
3ab1999b 3190 TYPE_DOMAIN (type));
3932261a
MM
3191 return build_qualified_type (type, type_quals);
3192}
3193
3194/* Apply the TYPE_QUALS to the new DECL. */
3195
3196void
3197c_apply_type_quals_to_decl (type_quals, decl)
3198 int type_quals;
3199 tree decl;
3200{
3201 if (type_quals & TYPE_QUAL_CONST)
3202 TREE_READONLY (decl) = 1;
3203 if (type_quals & TYPE_QUAL_VOLATILE)
3204 {
3205 TREE_SIDE_EFFECTS (decl) = 1;
3206 TREE_THIS_VOLATILE (decl) = 1;
3207 }
6946bc60 3208 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 3209 {
6946bc60
MM
3210 if (!TREE_TYPE (decl)
3211 || !POINTER_TYPE_P (TREE_TYPE (decl))
3212 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3213 error ("invalid use of `restrict'");
3214 else if (flag_strict_aliasing)
3932261a 3215 {
6946bc60
MM
3216 /* No two restricted pointers can point at the same thing.
3217 However, a restricted pointer can point at the same thing
3218 as an unrestricted pointer, if that unrestricted pointer
3219 is based on the restricted pointer. So, we make the
3220 alias set for the restricted pointer a subset of the
3221 alias set for the type pointed to by the type of the
3222 decl. */
3223
3224 int pointed_to_alias_set
3225 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3226
3227 if (!pointed_to_alias_set)
3228 /* It's not legal to make a subset of alias set zero. */
3229 ;
3230 else
3231 {
3232 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3233 record_alias_subset (pointed_to_alias_set,
3234 DECL_POINTER_ALIAS_SET (decl));
3235 }
3932261a
MM
3236 }
3237 }
3238}
3239
3240/* T is an expression with pointer type. Find the DECL on which this
3241 expression is based. (For example, in `a[i]' this would be `a'.)
3242 If there is no such DECL, or a unique decl cannot be determined,
3243 NULL_TREE is retured. */
3244
3245static tree
3246c_find_base_decl (t)
3247 tree t;
3248{
3249 int i;
3250 tree decl;
3251
3252 if (t == NULL_TREE || t == error_mark_node)
3253 return NULL_TREE;
3254
3255 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3256 return NULL_TREE;
3257
3258 decl = NULL_TREE;
3259
3260 if (TREE_CODE (t) == FIELD_DECL
3261 || TREE_CODE (t) == PARM_DECL
3262 || TREE_CODE (t) == VAR_DECL)
3263 /* Aha, we found a pointer-typed declaration. */
3264 return t;
3265
3266 /* It would be nice to deal with COMPONENT_REFs here. If we could
3267 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3268 also the same. */
3269
3270 /* Handle general expressions. */
3271 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3272 {
3273 case '1':
3274 case '2':
3275 case '3':
3276 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3277 {
3278 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3279 if (d)
3280 {
3281 if (!decl)
3282 decl = d;
3283 else if (d && d != decl)
3284 /* Two different declarations. That's confusing; let's
3285 just assume we don't know what's going on. */
3286 decl = NULL_TREE;
3287 }
3288 }
3289 break;
3290
3291 default:
3292 break;
3293 }
3294
3295 return decl;
0b73773c 3296}
41472af8
MM
3297
3298/* Return the typed-based alias set for T, which may be an expression
3299 or a type. */
3300
3301int
3302c_get_alias_set (t)
3303 tree t;
3304{
41472af8 3305 tree type;
08bc2431 3306 tree u;
41472af8
MM
3307
3308 if (t == error_mark_node)
3309 return 0;
3310
3311 type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
ece32014 3312 ? t : TREE_TYPE (t);
41472af8
MM
3313
3314 if (type == error_mark_node)
3315 return 0;
3316
ece32014
MM
3317 /* Deal with special cases first; for certain kinds of references
3318 we're interested in more than just the type. */
41472af8
MM
3319
3320 if (TREE_CODE (t) == BIT_FIELD_REF)
3321 /* Perhaps reads and writes to this piece of data alias fields
3322 neighboring the bitfield. Perhaps that's impossible. For now,
3323 let's just assume that bitfields can alias everything, which is
3324 the conservative assumption. */
3325 return 0;
ece32014 3326
08bc2431
MM
3327 /* Permit type-punning when accessing a union, provided the access
3328 is directly through the union. For example, this code does not
3329 permit taking the address of a union member and then storing
3330 through it. Even the type-punning allowed here is a GCC
3331 extension, albeit a common and useful one; the C standard says
3332 that such accesses have implementation-defined behavior. */
3333 for (u = t;
3334 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3335 u = TREE_OPERAND (u, 0))
3336 if (TREE_CODE (u) == COMPONENT_REF
3337 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3338 return 0;
ece32014 3339
3932261a
MM
3340 if (TREE_CODE (t) == INDIRECT_REF)
3341 {
3342 /* Check for accesses through restrict-qualified pointers. */
3343 tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3344
3345 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3346 /* We use the alias set indicated in the declaration. */
3347 return DECL_POINTER_ALIAS_SET (decl);
3348 }
3349
ece32014
MM
3350 /* From here on, only the type matters. */
3351
08bc2431
MM
3352 if (TREE_CODE (t) == COMPONENT_REF
3353 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3354 /* Since build_modify_expr calls get_unwidened for stores to
3355 component references, the type of a bit field can be changed
3356 from (say) `unsigned int : 16' to `unsigned short' or from
3357 `enum E : 16' to `short'. We want the real type of the
3358 bit-field in this case, not some the integral equivalent. */
3359 type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3360
ece32014
MM
3361 if (TYPE_ALIAS_SET_KNOWN_P (type))
3362 /* If we've already calculated the value, just return it. */
3363 return TYPE_ALIAS_SET (type);
41472af8 3364 else if (TYPE_MAIN_VARIANT (type) != type)
ece32014
MM
3365 /* The C standard specifically allows aliasing between
3366 cv-qualified variants of types. */
3367 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
41472af8
MM
3368 else if (TREE_CODE (type) == INTEGER_TYPE)
3369 {
3370 tree signed_variant;
3371
3372 /* The C standard specifically allows aliasing between signed and
3373 unsigned variants of the same type. We treat the signed
3374 variant as canonical. */
3375 signed_variant = signed_type (type);
3376
3377 if (signed_variant != type)
ece32014 3378 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
41472af8
MM
3379 else if (signed_variant == signed_char_type_node)
3380 /* The C standard guarantess that any object may be accessed
3381 via an lvalue that has character type. We don't have to
3382 check for unsigned_char_type_node or char_type_node because
3383 we are specifically looking at the signed variant. */
ece32014 3384 TYPE_ALIAS_SET (type) = 0;
41472af8 3385 }
ece32014
MM
3386 else if (TREE_CODE (type) == ARRAY_TYPE)
3387 /* Anything that can alias one of the array elements can alias
3388 the entire array as well. */
3389 TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3390 else if (TREE_CODE (type) == FUNCTION_TYPE)
3391 /* There are no objects of FUNCTION_TYPE, so there's no point in
3392 using up an alias set for them. (There are, of course,
3393 pointers and references to functions, but that's
3394 different.) */
3395 TYPE_ALIAS_SET (type) = 0;
41472af8
MM
3396 else if (TREE_CODE (type) == RECORD_TYPE
3397 || TREE_CODE (type) == UNION_TYPE)
ece32014
MM
3398 /* If TYPE is a struct or union type then we're reading or
3399 writing an entire struct. Thus, we don't know anything about
3400 aliasing. (In theory, such an access can only alias objects
3401 whose type is the same as one of the fields, recursively, but
3402 we don't yet make any use of that information.) */
3403 TYPE_ALIAS_SET (type) = 0;
3404
3405 if (!TYPE_ALIAS_SET_KNOWN_P (type))
3406 /* TYPE is something we haven't seen before. Put it in a new
3407 alias set. */
3408 TYPE_ALIAS_SET (type) = new_alias_set ();
41472af8 3409
41472af8
MM
3410 return TYPE_ALIAS_SET (type);
3411}
7f4edbcb
BS
3412
3413/* Build tree nodes and builtin functions common to both C and C++ language
3414 frontends.
3415 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
3416 some stricter prototypes in that case.
3417 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
3418 the language frontend flags flag_no_builtin and
3419 flag_no_nonansi_builtin. */
3420void
3421c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
3422 int cplus_mode, no_builtins, no_nonansi_builtins;
3423{
3424 tree temp;
3425 tree memcpy_ftype, memset_ftype, strlen_ftype;
3426 tree endlink, int_endlink, double_endlink, unsigned_endlink;
3427 tree sizetype_endlink;
3428 tree ptr_ftype, ptr_ftype_unsigned;
3429 tree void_ftype_any, void_ftype_int;
3430 tree double_ftype_double, double_ftype_double_double;
3431 tree float_ftype_float, ldouble_ftype_ldouble;
3432 tree int_ftype_cptr_cptr_sizet;
3433 tree int_ftype_string_string, string_ftype_ptr_ptr;
3434 tree long_ftype_long;
3435 /* Either char* or void*. */
3436 tree traditional_ptr_type_node;
d3707adb 3437 tree va_list_ptr_type_node;
daf68dd7 3438 tree va_list_arg_type_node;
d3707adb
RH
3439
3440#ifdef BUILD_VA_LIST_TYPE
3441 BUILD_VA_LIST_TYPE(va_list_type_node);
3442#else
3443 va_list_type_node = ptr_type_node;
3444#endif
3445 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3446 va_list_type_node));
daf68dd7 3447
d3707adb 3448 va_list_ptr_type_node = build_pointer_type (va_list_type_node);
7f4edbcb 3449
daf68dd7
RH
3450 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3451 va_list_arg_type_node = build_pointer_type (TREE_TYPE (va_list_type_node));
3452 else
3453 va_list_arg_type_node = va_list_type_node;
3454
7f4edbcb
BS
3455 endlink = void_list_node;
3456 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3457 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3458 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
3459
3460 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3461 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
3462 sizetype_endlink = tree_cons (NULL_TREE, sizetype, endlink);
3463 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3464 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3465
3466 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3467 void_ftype = build_function_type (void_type_node, endlink);
3468 void_ftype_int = build_function_type (void_type_node, int_endlink);
3469 void_ftype_ptr
3470 = build_function_type (void_type_node,
3471 tree_cons (NULL_TREE, ptr_type_node, endlink));
3472
3473 float_ftype_float
3474 = build_function_type (float_type_node,
3475 tree_cons (NULL_TREE, float_type_node, endlink));
3476
3477 double_ftype_double
3478 = build_function_type (double_type_node, double_endlink);
3479
3480 ldouble_ftype_ldouble
3481 = build_function_type (long_double_type_node,
3482 tree_cons (NULL_TREE, long_double_type_node,
3483 endlink));
3484
3485 double_ftype_double_double
3486 = build_function_type (double_type_node,
3487 tree_cons (NULL_TREE, double_type_node,
3488 double_endlink));
3489
3490 int_ftype_int
3491 = build_function_type (integer_type_node, int_endlink);
3492
3493 long_ftype_long
3494 = build_function_type (long_integer_type_node,
3495 tree_cons (NULL_TREE, long_integer_type_node,
3496 endlink));
3497
3498 int_ftype_cptr_cptr_sizet
3499 = build_function_type (integer_type_node,
3500 tree_cons (NULL_TREE, const_ptr_type_node,
3501 tree_cons (NULL_TREE, const_ptr_type_node,
3502 tree_cons (NULL_TREE,
3503 sizetype,
3504 endlink))));
3505
3506 /* Prototype for strcpy. */
3507 string_ftype_ptr_ptr
3508 = build_function_type (string_type_node,
3509 tree_cons (NULL_TREE, string_type_node,
3510 tree_cons (NULL_TREE,
3511 const_string_type_node,
3512 endlink)));
3513
3514 /* Prototype for strcmp. */
3515 int_ftype_string_string
3516 = build_function_type (integer_type_node,
3517 tree_cons (NULL_TREE, const_string_type_node,
3518 tree_cons (NULL_TREE,
3519 const_string_type_node,
3520 endlink)));
3521
3522 /* Prototype for strlen. */
3523 strlen_ftype
3524 = build_function_type ((flag_traditional && ! cplus_mode
3525 ? integer_type_node : sizetype),
3526 tree_cons (NULL_TREE, const_string_type_node,
3527 endlink));
3528
3529 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
3530 ? string_type_node : ptr_type_node);
3531
3532 /* Prototype for memcpy. */
3533 memcpy_ftype
3534 = build_function_type (traditional_ptr_type_node,
3535 tree_cons (NULL_TREE, ptr_type_node,
3536 tree_cons (NULL_TREE, const_ptr_type_node,
3537 sizetype_endlink)));
3538
3539 /* Prototype for memset. */
3540 memset_ftype
3541 = build_function_type (traditional_ptr_type_node,
3542 tree_cons (NULL_TREE, ptr_type_node,
3543 tree_cons (NULL_TREE, integer_type_node,
3544 tree_cons (NULL_TREE,
3545 sizetype,
3546 endlink))));
3547
3548 builtin_function ("__builtin_constant_p", default_function_type,
3549 BUILT_IN_CONSTANT_P, NULL_PTR);
3550
3551 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
3552 BUILT_IN_RETURN_ADDRESS, NULL_PTR);
3553
3554 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
3555 BUILT_IN_FRAME_ADDRESS, NULL_PTR);
3556
3557 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
3558 BUILT_IN_ALLOCA, "alloca");
3559 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
3560 /* Define alloca, ffs as builtins.
3561 Declare _exit just to mark it as volatile. */
3562 if (! no_builtins && ! no_nonansi_builtins)
3563 {
3564 temp = builtin_function ("alloca", ptr_ftype_sizetype,
3565 BUILT_IN_ALLOCA, NULL_PTR);
3566 /* Suppress error if redefined as a non-function. */
3567 DECL_BUILT_IN_NONANSI (temp) = 1;
3568 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
3569 /* Suppress error if redefined as a non-function. */
3570 DECL_BUILT_IN_NONANSI (temp) = 1;
3571 temp = builtin_function ("_exit", void_ftype_int,
3572 NOT_BUILT_IN, NULL_PTR);
3573 TREE_THIS_VOLATILE (temp) = 1;
3574 TREE_SIDE_EFFECTS (temp) = 1;
3575 /* Suppress error if redefined as a non-function. */
3576 DECL_BUILT_IN_NONANSI (temp) = 1;
3577 }
3578
3579 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
3580 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
3581 NULL_PTR);
3582 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
3583 NULL_PTR);
3584 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3585 NULL_PTR);
3586 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
3587 NULL_PTR);
3588 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
3589 NULL_PTR);
3590 builtin_function ("__builtin_classify_type", default_function_type,
3591 BUILT_IN_CLASSIFY_TYPE, NULL_PTR);
3592 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
3593 NULL_PTR);
3594 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
3595 NULL_PTR);
3596 builtin_function ("__builtin_setjmp",
3597 build_function_type (integer_type_node,
3598 tree_cons (NULL_TREE, ptr_type_node,
3599 endlink)),
3600 BUILT_IN_SETJMP, NULL_PTR);
3601 builtin_function ("__builtin_longjmp",
3602 build_function_type (void_type_node,
3603 tree_cons (NULL_TREE, ptr_type_node,
3604 tree_cons (NULL_TREE,
3605 integer_type_node,
3606 endlink))),
3607 BUILT_IN_LONGJMP, NULL_PTR);
3608 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP, NULL_PTR);
3609
3610 /* Untyped call and return. */
3611 builtin_function ("__builtin_apply_args", ptr_ftype,
3612 BUILT_IN_APPLY_ARGS, NULL_PTR);
3613
3614 temp = tree_cons (NULL_TREE,
3615 build_pointer_type (build_function_type (void_type_node,
3616 NULL_TREE)),
3617 tree_cons (NULL_TREE,
3618 ptr_type_node,
3619 tree_cons (NULL_TREE,
3620 sizetype,
3621 endlink)));
3622 builtin_function ("__builtin_apply",
3623 build_function_type (ptr_type_node, temp),
3624 BUILT_IN_APPLY, NULL_PTR);
3625 builtin_function ("__builtin_return", void_ftype_ptr,
3626 BUILT_IN_RETURN, NULL_PTR);
3627
d3707adb
RH
3628 /* Support for varargs.h and stdarg.h. */
3629 builtin_function ("__builtin_varargs_start",
3630 build_function_type (void_type_node,
3631 tree_cons (NULL_TREE,
3632 va_list_ptr_type_node,
3633 endlink)),
3634 BUILT_IN_VARARGS_START, NULL_PTR);
3635
3636 builtin_function ("__builtin_stdarg_start",
3637 build_function_type (void_type_node,
3638 tree_cons (NULL_TREE,
3639 va_list_ptr_type_node,
3640 NULL_TREE)),
3641 BUILT_IN_STDARG_START, NULL_PTR);
3642
3643 builtin_function ("__builtin_va_end",
3644 build_function_type (void_type_node,
3645 tree_cons (NULL_TREE,
daf68dd7 3646 va_list_arg_type_node,
d3707adb
RH
3647 endlink)),
3648 BUILT_IN_VA_END, NULL_PTR);
3649
3650 builtin_function ("__builtin_va_copy",
3651 build_function_type (void_type_node,
3652 tree_cons (NULL_TREE,
3653 va_list_ptr_type_node,
3654 tree_cons (NULL_TREE,
daf68dd7 3655 va_list_arg_type_node,
d3707adb
RH
3656 endlink))),
3657 BUILT_IN_VA_COPY, NULL_PTR);
3658
7f4edbcb
BS
3659 /* Currently under experimentation. */
3660 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3661 "memcpy");
3662 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
3663 BUILT_IN_MEMCMP, "memcmp");
3664 builtin_function ("__builtin_memset", memset_ftype, BUILT_IN_MEMSET,
3665 "memset");
3666 builtin_function ("__builtin_strcmp", int_ftype_string_string,
3667 BUILT_IN_STRCMP, "strcmp");
3668 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
3669 BUILT_IN_STRCPY, "strcpy");
3670 builtin_function ("__builtin_strlen", strlen_ftype,
3671 BUILT_IN_STRLEN, "strlen");
3672 builtin_function ("__builtin_sqrtf", float_ftype_float,
3673 BUILT_IN_FSQRT, "sqrtf");
3674 builtin_function ("__builtin_fsqrt", double_ftype_double,
3675 BUILT_IN_FSQRT, "sqrt");
3676 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
3677 BUILT_IN_FSQRT, "sqrtl");
3678 builtin_function ("__builtin_sinf", float_ftype_float,
3679 BUILT_IN_SIN, "sinf");
3680 builtin_function ("__builtin_sin", double_ftype_double,
3681 BUILT_IN_SIN, "sin");
3682 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
3683 BUILT_IN_SIN, "sinl");
3684 builtin_function ("__builtin_cosf", float_ftype_float,
3685 BUILT_IN_COS, "cosf");
3686 builtin_function ("__builtin_cos", double_ftype_double,
3687 BUILT_IN_COS, "cos");
3688 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
3689 BUILT_IN_COS, "cosl");
3690
3691 if (! no_builtins)
3692 {
3693 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
3694 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS, NULL_PTR);
3695 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, NULL_PTR);
3696 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3697 NULL_PTR);
3698 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, NULL_PTR);
3699 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, NULL_PTR);
3700 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
3701 NULL_PTR);
3702 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET, NULL_PTR);
3703 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
3704 NULL_PTR);
3705 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
3706 NULL_PTR);
3707 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN, NULL_PTR);
3708 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT, NULL_PTR);
3709 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT, NULL_PTR);
3710 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
3711 NULL_PTR);
3712 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN, NULL_PTR);
3713 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN, NULL_PTR);
3714 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN, NULL_PTR);
3715 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS, NULL_PTR);
3716 builtin_function ("cos", double_ftype_double, BUILT_IN_COS, NULL_PTR);
3717 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS, NULL_PTR);
3718
3719 /* Declare these functions volatile
3720 to avoid spurious "control drops through" warnings. */
3721 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
3722 NOT_BUILT_IN, NULL_PTR);
3723 TREE_THIS_VOLATILE (temp) = 1;
3724 TREE_SIDE_EFFECTS (temp) = 1;
3725
3726#if 0 /* ??? The C++ frontend used to do this. */
3727 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3728 them... */
3729 DECL_BUILT_IN_NONANSI (temp) = 1;
3730#endif
3731 temp = builtin_function ("exit",
3732 cplus_mode ? void_ftype_int : void_ftype_any,
3733 NOT_BUILT_IN, NULL_PTR);
3734 TREE_THIS_VOLATILE (temp) = 1;
3735 TREE_SIDE_EFFECTS (temp) = 1;
3736
3737#if 0 /* ??? The C++ frontend used to do this. */
3738 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3739 them... */
3740 DECL_BUILT_IN_NONANSI (temp) = 1;
3741#endif
3742 }
3743
3744#if 0
3745 /* Support for these has not been written in either expand_builtin
3746 or build_function_call. */
3747 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, NULL_PTR);
3748 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, NULL_PTR);
3749 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
3750 NULL_PTR);
3751 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
3752 NULL_PTR);
3753 builtin_function ("__builtin_fmod", double_ftype_double_double,
3754 BUILT_IN_FMOD, NULL_PTR);
3755 builtin_function ("__builtin_frem", double_ftype_double_double,
3756 BUILT_IN_FREM, NULL_PTR);
3757 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
3758 NULL_PTR);
3759 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
3760 NULL_PTR);
3761#endif
3762}
d3707adb
RH
3763
3764tree
3765build_va_arg (expr, type)
3766 tree expr, type;
3767{
3768 return build1 (VA_ARG_EXPR, type, expr);
3769}
7e8176d7
RH
3770
3771/* Return nonzero if VALUE is a valid constant-valued expression
3772 for use in initializing a static variable; one that can be an
3773 element of a "constant" initializer.
3774
3775 Return null_pointer_node if the value is absolute;
3776 if it is relocatable, return the variable that determines the relocation.
3777 We assume that VALUE has been folded as much as possible;
3778 therefore, we do not need to check for such things as
3779 arithmetic-combinations of integers. */
3780
3781tree
3782initializer_constant_valid_p (value, endtype)
3783 tree value;
3784 tree endtype;
3785{
3786 switch (TREE_CODE (value))
3787 {
3788 case CONSTRUCTOR:
3789 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3790 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3791 && TREE_CONSTANT (value)
3792 && CONSTRUCTOR_ELTS (value))
3793 return
3794 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
3795 endtype);
3796
3797 return TREE_STATIC (value) ? null_pointer_node : 0;
3798
3799 case INTEGER_CST:
3800 case REAL_CST:
3801 case STRING_CST:
3802 case COMPLEX_CST:
3803 return null_pointer_node;
3804
3805 case ADDR_EXPR:
3806 return TREE_OPERAND (value, 0);
3807
3808 case NON_LVALUE_EXPR:
3809 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3810
3811 case CONVERT_EXPR:
3812 case NOP_EXPR:
3813 /* Allow conversions between pointer types. */
3814 if (POINTER_TYPE_P (TREE_TYPE (value))
3815 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3816 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3817
3818 /* Allow conversions between real types. */
3819 if (FLOAT_TYPE_P (TREE_TYPE (value))
3820 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3821 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3822
3823 /* Allow length-preserving conversions between integer types. */
3824 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3825 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))
3826 && (TYPE_PRECISION (TREE_TYPE (value))
3827 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3828 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3829
3830 /* Allow conversions between other integer types only if
3831 explicit value. */
3832 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3833 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3834 {
3835 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3836 endtype);
3837 if (inner == null_pointer_node)
3838 return null_pointer_node;
3839 break;
3840 }
3841
3842 /* Allow (int) &foo provided int is as wide as a pointer. */
3843 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3844 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))
3845 && (TYPE_PRECISION (TREE_TYPE (value))
3846 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3847 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3848 endtype);
3849
3850 /* Likewise conversions from int to pointers, but also allow
3851 conversions from 0. */
3852 if (POINTER_TYPE_P (TREE_TYPE (value))
3853 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3854 {
3855 if (integer_zerop (TREE_OPERAND (value, 0)))
3856 return null_pointer_node;
3857 else if (TYPE_PRECISION (TREE_TYPE (value))
3858 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))
3859 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3860 endtype);
3861 }
3862
3863 /* Allow conversions to union types if the value inside is okay. */
3864 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
3865 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3866 endtype);
3867 break;
3868
3869 case PLUS_EXPR:
3870 if (! INTEGRAL_TYPE_P (endtype)
3871 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3872 {
3873 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3874 endtype);
3875 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3876 endtype);
3877 /* If either term is absolute, use the other terms relocation. */
3878 if (valid0 == null_pointer_node)
3879 return valid1;
3880 if (valid1 == null_pointer_node)
3881 return valid0;
3882 }
3883 break;
3884
3885 case MINUS_EXPR:
3886 if (! INTEGRAL_TYPE_P (endtype)
3887 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3888 {
3889 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3890 endtype);
3891 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3892 endtype);
3893 /* Win if second argument is absolute. */
3894 if (valid1 == null_pointer_node)
3895 return valid0;
3896 /* Win if both arguments have the same relocation.
3897 Then the value is absolute. */
3898 if (valid0 == valid1)
3899 return null_pointer_node;
3900 }
3901
3902 /* Support differences between labels. */
3903 if (INTEGRAL_TYPE_P (endtype))
3904 {
3905 tree op0, op1;
3906 op0 = TREE_OPERAND (value, 0);
3907 op1 = TREE_OPERAND (value, 1);
3908 STRIP_NOPS (op0);
3909 STRIP_NOPS (op1);
3910
3911 if (TREE_CODE (op0) == ADDR_EXPR
3912 && TREE_CODE (TREE_OPERAND (op0, 0)) == LABEL_DECL
3913 && TREE_CODE (op1) == ADDR_EXPR
3914 && TREE_CODE (TREE_OPERAND (op1, 0)) == LABEL_DECL)
3915 return null_pointer_node;
3916 }
3917 break;
3918
3919 default:
3920 break;
3921 }
3922
3923 return 0;
3924}
3925
This page took 0.847225 seconds and 5 git commands to generate.