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