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