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