]>
Commit | Line | Data |
---|---|---|
b30f223b | 1 | /* Subroutines shared by all languages that are variants of C. |
09f89307 | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 |
06ceef4e | 3 | Free Software Foundation, Inc. |
b30f223b RS |
4 | |
5 | This file is part of GNU CC. | |
6 | ||
7 | GNU CC is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GNU CC is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU CC; see the file COPYING. If not, write to | |
940d9d63 RK |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
b30f223b RS |
21 | |
22 | #include "config.h" | |
670ee920 | 23 | #include "system.h" |
b30f223b | 24 | #include "tree.h" |
b30f223b | 25 | #include "flags.h" |
5f6da302 | 26 | #include "toplev.h" |
d6f4ec51 | 27 | #include "output.h" |
e2af664c | 28 | #include "c-pragma.h" |
3932261a | 29 | #include "rtl.h" |
1526a060 | 30 | #include "ggc.h" |
c6991660 | 31 | #include "expr.h" |
8f17b5c5 | 32 | #include "c-common.h" |
7bdb32b9 | 33 | #include "tm_p.h" |
235cfbc4 | 34 | #include "obstack.h" |
c8724862 | 35 | #include "cpplib.h" |
9ba2e1ef | 36 | cpp_reader *parse_in; /* Declared in c-lex.h. */ |
c8724862 | 37 | |
12a39b12 JM |
38 | #undef WCHAR_TYPE_SIZE |
39 | #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node) | |
fef610be | 40 | |
eaa7c03f JM |
41 | /* We let tm.h override the types used here, to handle trivial differences |
42 | such as the choice of unsigned int or long unsigned int for size_t. | |
43 | When machines start needing nontrivial differences in the size type, | |
44 | it would be best to do something here to figure out automatically | |
45 | from other information what type to use. */ | |
46 | ||
47 | #ifndef SIZE_TYPE | |
48 | #define SIZE_TYPE "long unsigned int" | |
49 | #endif | |
50 | ||
51 | #ifndef WCHAR_TYPE | |
52 | #define WCHAR_TYPE "int" | |
53 | #endif | |
54 | ||
0884b60c BS |
55 | #ifndef PTRDIFF_TYPE |
56 | #define PTRDIFF_TYPE "long int" | |
57 | #endif | |
58 | ||
5fd8e536 JM |
59 | #ifndef WINT_TYPE |
60 | #define WINT_TYPE "unsigned int" | |
61 | #endif | |
62 | ||
63 | #ifndef INTMAX_TYPE | |
64 | #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
65 | ? "int" \ | |
66 | : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
67 | ? "long int" \ | |
68 | : "long long int")) | |
69 | #endif | |
70 | ||
71 | #ifndef UINTMAX_TYPE | |
72 | #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
73 | ? "unsigned int" \ | |
74 | : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
75 | ? "long unsigned int" \ | |
76 | : "long long unsigned int")) | |
77 | #endif | |
78 | ||
7f4edbcb | 79 | /* The following symbols are subsumed in the c_global_trees array, and |
d125d268 | 80 | listed here individually for documentation purposes. |
7f4edbcb BS |
81 | |
82 | INTEGER_TYPE and REAL_TYPE nodes for the standard data types. | |
83 | ||
84 | tree short_integer_type_node; | |
85 | tree long_integer_type_node; | |
86 | tree long_long_integer_type_node; | |
87 | ||
88 | tree short_unsigned_type_node; | |
89 | tree long_unsigned_type_node; | |
90 | tree long_long_unsigned_type_node; | |
91 | ||
92 | tree boolean_type_node; | |
93 | tree boolean_false_node; | |
94 | tree boolean_true_node; | |
95 | ||
96 | tree ptrdiff_type_node; | |
97 | ||
98 | tree unsigned_char_type_node; | |
99 | tree signed_char_type_node; | |
100 | tree wchar_type_node; | |
101 | tree signed_wchar_type_node; | |
102 | tree unsigned_wchar_type_node; | |
103 | ||
104 | tree float_type_node; | |
105 | tree double_type_node; | |
106 | tree long_double_type_node; | |
107 | ||
108 | tree complex_integer_type_node; | |
109 | tree complex_float_type_node; | |
110 | tree complex_double_type_node; | |
111 | tree complex_long_double_type_node; | |
112 | ||
113 | tree intQI_type_node; | |
114 | tree intHI_type_node; | |
115 | tree intSI_type_node; | |
116 | tree intDI_type_node; | |
117 | tree intTI_type_node; | |
118 | ||
119 | tree unsigned_intQI_type_node; | |
120 | tree unsigned_intHI_type_node; | |
121 | tree unsigned_intSI_type_node; | |
122 | tree unsigned_intDI_type_node; | |
123 | tree unsigned_intTI_type_node; | |
124 | ||
125 | tree widest_integer_literal_type_node; | |
126 | tree widest_unsigned_literal_type_node; | |
127 | ||
128 | Nodes for types `void *' and `const void *'. | |
129 | ||
130 | tree ptr_type_node, const_ptr_type_node; | |
131 | ||
132 | Nodes for types `char *' and `const char *'. | |
133 | ||
134 | tree string_type_node, const_string_type_node; | |
135 | ||
136 | Type `char[SOMENUMBER]'. | |
137 | Used when an array of char is needed and the size is irrelevant. | |
138 | ||
139 | tree char_array_type_node; | |
140 | ||
141 | Type `int[SOMENUMBER]' or something like it. | |
142 | Used when an array of int needed and the size is irrelevant. | |
143 | ||
144 | tree int_array_type_node; | |
145 | ||
146 | Type `wchar_t[SOMENUMBER]' or something like it. | |
147 | Used when a wide string literal is created. | |
148 | ||
149 | tree wchar_array_type_node; | |
150 | ||
151 | Type `int ()' -- used for implicit declaration of functions. | |
152 | ||
153 | tree default_function_type; | |
154 | ||
155 | Function types `int (int)', etc. | |
156 | ||
157 | tree int_ftype_int; | |
158 | tree void_ftype; | |
159 | tree void_ftype_ptr; | |
160 | tree int_ftype_int; | |
161 | tree ptr_ftype_sizetype; | |
162 | ||
163 | A VOID_TYPE node, packaged in a TREE_LIST. | |
164 | ||
165 | tree void_list_node; | |
166 | ||
63ad61ed ZW |
167 | The identifiers __FUNCTION__, __PRETTY_FUNCTION__, and __func__. |
168 | ||
169 | tree function_id_node; | |
170 | tree pretty_function_id_node; | |
171 | tree func_id_node; | |
172 | ||
7f4edbcb BS |
173 | */ |
174 | ||
175 | tree c_global_trees[CTI_MAX]; | |
0b73773c | 176 | |
6bcedb4e MM |
177 | /* Nonzero means don't recognize the non-ANSI builtin functions. */ |
178 | ||
179 | int flag_no_builtin; | |
180 | ||
181 | /* Nonzero means don't recognize the non-ANSI builtin functions. | |
182 | -ansi sets this. */ | |
183 | ||
184 | int flag_no_nonansi_builtin; | |
185 | ||
eaa7c03f JM |
186 | /* Nonzero means give `double' the same size as `float'. */ |
187 | ||
188 | int flag_short_double; | |
189 | ||
190 | /* Nonzero means give `wchar_t' the same size as `short'. */ | |
191 | ||
192 | int flag_short_wchar; | |
193 | ||
0a7394bc MM |
194 | /* If non-NULL, dump the tree structure for the entire translation |
195 | unit to this file. */ | |
196 | ||
197 | const char *flag_dump_translation_unit; | |
198 | ||
2683ed8d BS |
199 | /* Nonzero means warn about possible violations of sequence point rules. */ |
200 | ||
201 | int warn_sequence_point; | |
202 | ||
f09f1de5 MM |
203 | /* The elements of `ridpointers' are identifier nodes for the reserved |
204 | type names and storage classes. It is indexed by a RID_... value. */ | |
205 | tree *ridpointers; | |
206 | ||
2ce07e2d NS |
207 | tree (*make_fname_decl) PARAMS ((tree, const char *, int)); |
208 | ||
ae499cce MM |
209 | /* If non-NULL, the address of a language-specific function that |
210 | returns 1 for language-specific statement codes. */ | |
211 | int (*lang_statement_code_p) PARAMS ((enum tree_code)); | |
212 | ||
8f17b5c5 MM |
213 | /* If non-NULL, the address of a language-specific function that takes |
214 | any action required right before expand_function_end is called. */ | |
215 | void (*lang_expand_function_end) PARAMS ((void)); | |
216 | ||
67673f5c MM |
217 | /* If this variable is defined to a non-NULL value, it will be called |
218 | after the file has been completely parsed. */ | |
219 | void (*back_end_hook) PARAMS ((tree)); | |
220 | ||
e78a3b42 RK |
221 | /* Nonzero means the expression being parsed will never be evaluated. |
222 | This is a count, since unevaluated expressions can nest. */ | |
223 | int skip_evaluation; | |
224 | ||
2786cbad | 225 | enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION, |
7d384cc0 | 226 | A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION, |
bbb1ae01 | 227 | A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED, |
a157febd | 228 | A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC, |
2a8f6b90 | 229 | A_NO_LIMIT_STACK, A_PURE}; |
53065596 | 230 | |
6e090c76 KG |
231 | static void add_attribute PARAMS ((enum attrs, const char *, |
232 | int, int, int)); | |
233 | static void init_attributes PARAMS ((void)); | |
6e090c76 | 234 | static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree)); |
d02b54f6 | 235 | static int constant_fits_type_p PARAMS ((tree, tree)); |
4724b3de | 236 | |
0a7ed33c BS |
237 | /* Keep a stack of if statements. We record the number of compound |
238 | statements seen up to the if keyword, as well as the line number | |
239 | and file of the if. If a potentially ambiguous else is seen, that | |
240 | fact is recorded; the warning is issued when we can be sure that | |
241 | the enclosing if statement does not have an else branch. */ | |
242 | typedef struct | |
243 | { | |
244 | int compstmt_count; | |
245 | int line; | |
dff01034 | 246 | const char *file; |
0a7ed33c | 247 | int needs_warning; |
8f17b5c5 | 248 | tree if_stmt; |
0a7ed33c BS |
249 | } if_elt; |
250 | ||
251 | static if_elt *if_stack; | |
6d819282 MK |
252 | |
253 | /* Amount of space in the if statement stack. */ | |
254 | static int if_stack_space = 0; | |
255 | ||
256 | /* Stack pointer. */ | |
257 | static int if_stack_pointer = 0; | |
258 | ||
8f17b5c5 | 259 | /* Record the start of an if-then, and record the start of it |
0a7ed33c BS |
260 | for ambiguous else detection. */ |
261 | ||
6d819282 | 262 | void |
8f17b5c5 | 263 | c_expand_start_cond (cond, compstmt_count) |
6d819282 | 264 | tree cond; |
6d819282 MK |
265 | int compstmt_count; |
266 | { | |
8f17b5c5 MM |
267 | tree if_stmt; |
268 | ||
6d819282 MK |
269 | /* Make sure there is enough space on the stack. */ |
270 | if (if_stack_space == 0) | |
271 | { | |
272 | if_stack_space = 10; | |
0a7ed33c | 273 | if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt)); |
6d819282 MK |
274 | } |
275 | else if (if_stack_space == if_stack_pointer) | |
276 | { | |
277 | if_stack_space += 10; | |
0a7ed33c | 278 | if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt)); |
6d819282 | 279 | } |
0a7ed33c | 280 | |
8f17b5c5 MM |
281 | if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE); |
282 | IF_COND (if_stmt) = cond; | |
283 | add_stmt (if_stmt); | |
284 | ||
6d819282 | 285 | /* Record this if statement. */ |
0a7ed33c BS |
286 | if_stack[if_stack_pointer].compstmt_count = compstmt_count; |
287 | if_stack[if_stack_pointer].file = input_filename; | |
288 | if_stack[if_stack_pointer].line = lineno; | |
289 | if_stack[if_stack_pointer].needs_warning = 0; | |
8f17b5c5 | 290 | if_stack[if_stack_pointer].if_stmt = if_stmt; |
0a7ed33c | 291 | if_stack_pointer++; |
8f17b5c5 | 292 | } |
6d819282 | 293 | |
8f17b5c5 MM |
294 | /* Called after the then-clause for an if-statement is processed. */ |
295 | ||
296 | void | |
297 | c_finish_then () | |
298 | { | |
299 | tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt; | |
300 | RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt)); | |
6d819282 MK |
301 | } |
302 | ||
8f17b5c5 | 303 | /* Record the end of an if-then. Optionally warn if a nested |
0a7ed33c BS |
304 | if statement had an ambiguous else clause. */ |
305 | ||
6d819282 MK |
306 | void |
307 | c_expand_end_cond () | |
308 | { | |
309 | if_stack_pointer--; | |
0a7ed33c BS |
310 | if (if_stack[if_stack_pointer].needs_warning) |
311 | warning_with_file_and_line (if_stack[if_stack_pointer].file, | |
312 | if_stack[if_stack_pointer].line, | |
313 | "suggest explicit braces to avoid ambiguous `else'"); | |
8f17b5c5 | 314 | last_expr_type = NULL_TREE; |
6d819282 MK |
315 | } |
316 | ||
8f17b5c5 | 317 | /* Called between the then-clause and the else-clause |
0a7ed33c BS |
318 | of an if-then-else. */ |
319 | ||
6d819282 MK |
320 | void |
321 | c_expand_start_else () | |
322 | { | |
0a7ed33c BS |
323 | /* An ambiguous else warning must be generated for the enclosing if |
324 | statement, unless we see an else branch for that one, too. */ | |
6d819282 MK |
325 | if (warn_parentheses |
326 | && if_stack_pointer > 1 | |
0a7ed33c BS |
327 | && (if_stack[if_stack_pointer - 1].compstmt_count |
328 | == if_stack[if_stack_pointer - 2].compstmt_count)) | |
329 | if_stack[if_stack_pointer - 2].needs_warning = 1; | |
330 | ||
331 | /* Even if a nested if statement had an else branch, it can't be | |
332 | ambiguous if this one also has an else. So don't warn in that | |
333 | case. Also don't warn for any if statements nested in this else. */ | |
334 | if_stack[if_stack_pointer - 1].needs_warning = 0; | |
335 | if_stack[if_stack_pointer - 1].compstmt_count--; | |
8f17b5c5 MM |
336 | } |
337 | ||
338 | /* Called after the else-clause for an if-statement is processed. */ | |
6d819282 | 339 | |
8f17b5c5 MM |
340 | void |
341 | c_finish_else () | |
342 | { | |
343 | tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt; | |
344 | RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt)); | |
6d819282 MK |
345 | } |
346 | ||
6f4d7222 | 347 | /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */ |
7da551a2 RS |
348 | |
349 | void | |
350 | declare_function_name () | |
351 | { | |
dff01034 | 352 | const char *name, *printable_name; |
7da551a2 RS |
353 | |
354 | if (current_function_decl == NULL) | |
355 | { | |
356 | name = ""; | |
357 | printable_name = "top level"; | |
358 | } | |
359 | else | |
360 | { | |
6152f876 RS |
361 | /* Allow functions to be nameless (such as artificial ones). */ |
362 | if (DECL_NAME (current_function_decl)) | |
363 | name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl)); | |
364 | else | |
365 | name = ""; | |
a1d7ffe3 | 366 | printable_name = (*decl_printable_name) (current_function_decl, 2); |
63ad61ed ZW |
367 | |
368 | /* ISO C99 defines __func__, which is a variable, not a string | |
369 | constant, and which is not a defined symbol at file scope. */ | |
370 | (*make_fname_decl) (func_id_node, name, 0); | |
7da551a2 | 371 | } |
2ce07e2d | 372 | |
63ad61ed ZW |
373 | (*make_fname_decl) (function_id_node, name, 0); |
374 | (*make_fname_decl) (pretty_function_id_node, printable_name, 1); | |
7da551a2 RS |
375 | } |
376 | ||
b30f223b RS |
377 | /* Given a chain of STRING_CST nodes, |
378 | concatenate them into one STRING_CST | |
379 | and give it a suitable array-of-chars data type. */ | |
380 | ||
381 | tree | |
382 | combine_strings (strings) | |
383 | tree strings; | |
384 | { | |
385 | register tree value, t; | |
386 | register int length = 1; | |
387 | int wide_length = 0; | |
388 | int wide_flag = 0; | |
389 | int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT; | |
390 | int nchars; | |
1326a48b | 391 | const int nchars_max = flag_isoc99 ? 4095 : 509; |
b30f223b RS |
392 | |
393 | if (TREE_CHAIN (strings)) | |
394 | { | |
395 | /* More than one in the chain, so concatenate. */ | |
396 | register char *p, *q; | |
397 | ||
398 | /* Don't include the \0 at the end of each substring, | |
399 | except for the last one. | |
400 | Count wide strings and ordinary strings separately. */ | |
401 | for (t = strings; t; t = TREE_CHAIN (t)) | |
402 | { | |
403 | if (TREE_TYPE (t) == wchar_array_type_node) | |
404 | { | |
405 | wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes); | |
406 | wide_flag = 1; | |
407 | } | |
408 | else | |
409 | length += (TREE_STRING_LENGTH (t) - 1); | |
410 | } | |
411 | ||
412 | /* If anything is wide, the non-wides will be converted, | |
413 | which makes them take more space. */ | |
414 | if (wide_flag) | |
415 | length = length * wchar_bytes + wide_length; | |
416 | ||
520a57c8 | 417 | p = alloca (length); |
b30f223b RS |
418 | |
419 | /* Copy the individual strings into the new combined string. | |
420 | If the combined string is wide, convert the chars to ints | |
421 | for any individual strings that are not wide. */ | |
422 | ||
423 | q = p; | |
424 | for (t = strings; t; t = TREE_CHAIN (t)) | |
425 | { | |
426 | int len = (TREE_STRING_LENGTH (t) | |
427 | - ((TREE_TYPE (t) == wchar_array_type_node) | |
428 | ? wchar_bytes : 1)); | |
429 | if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag) | |
430 | { | |
7e2231e7 | 431 | memcpy (q, TREE_STRING_POINTER (t), len); |
b30f223b RS |
432 | q += len; |
433 | } | |
434 | else | |
435 | { | |
436 | int i; | |
437 | for (i = 0; i < len; i++) | |
41bbd14e JW |
438 | { |
439 | if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT) | |
440 | ((short *) q)[i] = TREE_STRING_POINTER (t)[i]; | |
441 | else | |
442 | ((int *) q)[i] = TREE_STRING_POINTER (t)[i]; | |
443 | } | |
b30f223b RS |
444 | q += len * wchar_bytes; |
445 | } | |
446 | } | |
447 | if (wide_flag) | |
448 | { | |
449 | int i; | |
450 | for (i = 0; i < wchar_bytes; i++) | |
451 | *q++ = 0; | |
452 | } | |
453 | else | |
454 | *q = 0; | |
455 | ||
520a57c8 | 456 | value = build_string (length, p); |
b30f223b RS |
457 | } |
458 | else | |
459 | { | |
460 | value = strings; | |
461 | length = TREE_STRING_LENGTH (value); | |
462 | if (TREE_TYPE (value) == wchar_array_type_node) | |
463 | wide_flag = 1; | |
464 | } | |
465 | ||
b57062ca | 466 | /* Compute the number of elements, for the array type. */ |
b30f223b RS |
467 | nchars = wide_flag ? length / wchar_bytes : length; |
468 | ||
690c96c8 | 469 | if (pedantic && nchars - 1 > nchars_max && c_language == clk_c) |
3220116f | 470 | pedwarn ("string length `%d' is greater than the length `%d' ISO C%d compilers are required to support", |
690c96c8 | 471 | nchars - 1, nchars_max, flag_isoc99 ? 99 : 89); |
1326a48b | 472 | |
b30f223b RS |
473 | /* Create the array type for the string constant. |
474 | -Wwrite-strings says make the string constant an array of const char | |
d9cf7c82 JM |
475 | so that copying it to a non-const pointer will get a warning. |
476 | For C++, this is the standard behavior. */ | |
477 | if (flag_const_strings | |
b30f223b RS |
478 | && (! flag_traditional && ! flag_writable_strings)) |
479 | { | |
480 | tree elements | |
481 | = build_type_variant (wide_flag ? wchar_type_node : char_type_node, | |
482 | 1, 0); | |
483 | TREE_TYPE (value) | |
484 | = build_array_type (elements, | |
485 | build_index_type (build_int_2 (nchars - 1, 0))); | |
486 | } | |
487 | else | |
488 | TREE_TYPE (value) | |
489 | = build_array_type (wide_flag ? wchar_type_node : char_type_node, | |
490 | build_index_type (build_int_2 (nchars - 1, 0))); | |
d9cf7c82 | 491 | |
ccd4c832 JM |
492 | TREE_CONSTANT (value) = 1; |
493 | TREE_READONLY (value) = ! flag_writable_strings; | |
b30f223b RS |
494 | TREE_STATIC (value) = 1; |
495 | return value; | |
496 | } | |
497 | \f | |
53065596 RK |
498 | /* To speed up processing of attributes, we maintain an array of |
499 | IDENTIFIER_NODES and the corresponding attribute types. */ | |
500 | ||
501 | /* Array to hold attribute information. */ | |
502 | ||
503 | static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50]; | |
504 | ||
505 | static int attrtab_idx = 0; | |
506 | ||
507 | /* Add an entry to the attribute table above. */ | |
508 | ||
509 | static void | |
510 | add_attribute (id, string, min_len, max_len, decl_req) | |
511 | enum attrs id; | |
dff01034 | 512 | const char *string; |
53065596 RK |
513 | int min_len, max_len; |
514 | int decl_req; | |
515 | { | |
516 | char buf[100]; | |
517 | ||
518 | attrtab[attrtab_idx].id = id; | |
519 | attrtab[attrtab_idx].name = get_identifier (string); | |
520 | attrtab[attrtab_idx].min = min_len; | |
521 | attrtab[attrtab_idx].max = max_len; | |
522 | attrtab[attrtab_idx++].decl_req = decl_req; | |
523 | ||
524 | sprintf (buf, "__%s__", string); | |
525 | ||
526 | attrtab[attrtab_idx].id = id; | |
527 | attrtab[attrtab_idx].name = get_identifier (buf); | |
528 | attrtab[attrtab_idx].min = min_len; | |
529 | attrtab[attrtab_idx].max = max_len; | |
530 | attrtab[attrtab_idx++].decl_req = decl_req; | |
531 | } | |
532 | ||
533 | /* Initialize attribute table. */ | |
534 | ||
535 | static void | |
536 | init_attributes () | |
537 | { | |
a89ca5c6 | 538 | add_attribute (A_PACKED, "packed", 0, 0, 0); |
bbb1ae01 | 539 | add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1); |
2786cbad | 540 | add_attribute (A_COMMON, "common", 0, 0, 1); |
53065596 RK |
541 | add_attribute (A_NORETURN, "noreturn", 0, 0, 1); |
542 | add_attribute (A_NORETURN, "volatile", 0, 0, 1); | |
6d819282 | 543 | add_attribute (A_UNUSED, "unused", 0, 0, 0); |
53065596 RK |
544 | add_attribute (A_CONST, "const", 0, 0, 1); |
545 | add_attribute (A_T_UNION, "transparent_union", 0, 0, 0); | |
546 | add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1); | |
547 | add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1); | |
548 | add_attribute (A_MODE, "mode", 1, 1, 1); | |
549 | add_attribute (A_SECTION, "section", 1, 1, 1); | |
550 | add_attribute (A_ALIGNED, "aligned", 0, 1, 0); | |
d161fae4 | 551 | add_attribute (A_FORMAT, "format", 3, 3, 1); |
0161e8da | 552 | add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1); |
4b8af8d9 JM |
553 | add_attribute (A_WEAK, "weak", 0, 0, 1); |
554 | add_attribute (A_ALIAS, "alias", 1, 1, 1); | |
07417085 | 555 | add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1); |
7d384cc0 | 556 | add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1); |
140592a0 | 557 | add_attribute (A_MALLOC, "malloc", 0, 0, 1); |
a157febd | 558 | add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1); |
2a8f6b90 | 559 | add_attribute (A_PURE, "pure", 0, 0, 1); |
53065596 RK |
560 | } |
561 | \f | |
adfaf194 JM |
562 | /* Default implementation of valid_lang_attribute, below. By default, there |
563 | are no language-specific attributes. */ | |
564 | ||
565 | static int | |
566 | default_valid_lang_attribute (attr_name, attr_args, decl, type) | |
567 | tree attr_name ATTRIBUTE_UNUSED; | |
568 | tree attr_args ATTRIBUTE_UNUSED; | |
569 | tree decl ATTRIBUTE_UNUSED; | |
570 | tree type ATTRIBUTE_UNUSED; | |
571 | { | |
572 | return 0; | |
573 | } | |
574 | ||
575 | /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific | |
576 | attribute for either declaration DECL or type TYPE and 0 otherwise. */ | |
577 | ||
6e090c76 | 578 | int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree)) |
adfaf194 JM |
579 | = default_valid_lang_attribute; |
580 | ||
1228e2a6 | 581 | /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES |
53065596 RK |
582 | and install them in NODE, which is either a DECL (including a TYPE_DECL) |
583 | or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers | |
0f41302f | 584 | and declaration modifiers but before the declaration proper. */ |
b30f223b RS |
585 | |
586 | void | |
53065596 RK |
587 | decl_attributes (node, attributes, prefix_attributes) |
588 | tree node, attributes, prefix_attributes; | |
b30f223b | 589 | { |
a16b4c9c JW |
590 | tree decl = 0, type = 0; |
591 | int is_type = 0; | |
53065596 RK |
592 | tree a; |
593 | ||
594 | if (attrtab_idx == 0) | |
595 | init_attributes (); | |
596 | ||
2f939d94 | 597 | if (DECL_P (node)) |
53065596 RK |
598 | { |
599 | decl = node; | |
600 | type = TREE_TYPE (decl); | |
601 | is_type = TREE_CODE (node) == TYPE_DECL; | |
602 | } | |
2f939d94 | 603 | else if (TYPE_P (node)) |
53065596 | 604 | type = node, is_type = 1; |
a2cd7b45 | 605 | |
e2af664c NC |
606 | #ifdef PRAGMA_INSERT_ATTRIBUTES |
607 | /* If the code in c-pragma.c wants to insert some attributes then | |
608 | allow it to do so. Do this before allowing machine back ends to | |
609 | insert attributes, so that they have the opportunity to override | |
610 | anything done here. */ | |
611 | PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes); | |
612 | #endif | |
d125d268 | 613 | |
f09db6e0 NC |
614 | #ifdef INSERT_ATTRIBUTES |
615 | INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes); | |
616 | #endif | |
d125d268 | 617 | |
eed32833 | 618 | attributes = chainon (prefix_attributes, attributes); |
5289b665 | 619 | |
b30f223b | 620 | for (a = attributes; a; a = TREE_CHAIN (a)) |
53065596 RK |
621 | { |
622 | tree name = TREE_PURPOSE (a); | |
623 | tree args = TREE_VALUE (a); | |
624 | int i; | |
625 | enum attrs id; | |
b57062ca | 626 | |
53065596 RK |
627 | for (i = 0; i < attrtab_idx; i++) |
628 | if (attrtab[i].name == name) | |
629 | break; | |
630 | ||
6eaba4a7 | 631 | if (i == attrtab_idx) |
53065596 | 632 | { |
adfaf194 JM |
633 | if (! valid_machine_attribute (name, args, decl, type) |
634 | && ! (* valid_lang_attribute) (name, args, decl, type)) | |
6eaba4a7 DE |
635 | warning ("`%s' attribute directive ignored", |
636 | IDENTIFIER_POINTER (name)); | |
17c1a44f SC |
637 | else if (decl != 0) |
638 | type = TREE_TYPE (decl); | |
53065596 RK |
639 | continue; |
640 | } | |
641 | else if (attrtab[i].decl_req && decl == 0) | |
642 | { | |
643 | warning ("`%s' attribute does not apply to types", | |
644 | IDENTIFIER_POINTER (name)); | |
645 | continue; | |
646 | } | |
647 | else if (list_length (args) < attrtab[i].min | |
648 | || list_length (args) > attrtab[i].max) | |
649 | { | |
650 | error ("wrong number of arguments specified for `%s' attribute", | |
651 | IDENTIFIER_POINTER (name)); | |
652 | continue; | |
653 | } | |
654 | ||
655 | id = attrtab[i].id; | |
656 | switch (id) | |
657 | { | |
658 | case A_PACKED: | |
1bcf5b08 | 659 | if (is_type) |
a89ca5c6 RK |
660 | TYPE_PACKED (type) = 1; |
661 | else if (TREE_CODE (decl) == FIELD_DECL) | |
53065596 RK |
662 | DECL_PACKED (decl) = 1; |
663 | /* We can't set DECL_PACKED for a VAR_DECL, because the bit is | |
664 | used for DECL_REGISTER. It wouldn't mean anything anyway. */ | |
665 | else | |
666 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
667 | break; | |
668 | ||
bbb1ae01 RK |
669 | case A_NOCOMMON: |
670 | if (TREE_CODE (decl) == VAR_DECL) | |
671 | DECL_COMMON (decl) = 0; | |
672 | else | |
673 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
674 | break; | |
675 | ||
2786cbad JM |
676 | case A_COMMON: |
677 | if (TREE_CODE (decl) == VAR_DECL) | |
678 | DECL_COMMON (decl) = 1; | |
679 | else | |
680 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
681 | break; | |
682 | ||
53065596 RK |
683 | case A_NORETURN: |
684 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
685 | TREE_THIS_VOLATILE (decl) = 1; | |
686 | else if (TREE_CODE (type) == POINTER_TYPE | |
687 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
b57062ca | 688 | TREE_TYPE (decl) = type |
53065596 RK |
689 | = build_pointer_type |
690 | (build_type_variant (TREE_TYPE (type), | |
691 | TREE_READONLY (TREE_TYPE (type)), 1)); | |
692 | else | |
693 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
694 | break; | |
695 | ||
140592a0 AG |
696 | case A_MALLOC: |
697 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
698 | DECL_IS_MALLOC (decl) = 1; | |
2a8f6b90 | 699 | /* ??? TODO: Support types. */ |
140592a0 AG |
700 | else |
701 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
702 | break; | |
703 | ||
1c4fadec | 704 | case A_UNUSED: |
6d819282 | 705 | if (is_type) |
d20a70b4 CM |
706 | if (decl) |
707 | TREE_USED (decl) = 1; | |
708 | else | |
709 | TREE_USED (type) = 1; | |
b57062ca | 710 | else if (TREE_CODE (decl) == PARM_DECL |
6d819282 | 711 | || TREE_CODE (decl) == VAR_DECL |
736b02fd KG |
712 | || TREE_CODE (decl) == FUNCTION_DECL |
713 | || TREE_CODE (decl) == LABEL_DECL) | |
1c4fadec RK |
714 | TREE_USED (decl) = 1; |
715 | else | |
716 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
717 | break; | |
718 | ||
53065596 RK |
719 | case A_CONST: |
720 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
721 | TREE_READONLY (decl) = 1; | |
722 | else if (TREE_CODE (type) == POINTER_TYPE | |
723 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
724 | TREE_TYPE (decl) = type | |
725 | = build_pointer_type | |
726 | (build_type_variant (TREE_TYPE (type), 1, | |
727 | TREE_THIS_VOLATILE (TREE_TYPE (type)))); | |
728 | else | |
729 | warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
730 | break; | |
731 | ||
2a8f6b90 JH |
732 | case A_PURE: |
733 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
734 | DECL_IS_PURE (decl) = 1; | |
735 | /* ??? TODO: Support types. */ | |
736 | else | |
737 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
738 | break; | |
739 | ||
740 | ||
53065596 | 741 | case A_T_UNION: |
1bcf5b08 | 742 | if (is_type |
53065596 | 743 | && TREE_CODE (type) == UNION_TYPE |
1bcf5b08 | 744 | && (decl == 0 |
62b1077c RK |
745 | || (TYPE_FIELDS (type) != 0 |
746 | && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type))))) | |
1bcf5b08 RK |
747 | TYPE_TRANSPARENT_UNION (type) = 1; |
748 | else if (decl != 0 && TREE_CODE (decl) == PARM_DECL | |
53065596 RK |
749 | && TREE_CODE (type) == UNION_TYPE |
750 | && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type))) | |
1bcf5b08 | 751 | DECL_TRANSPARENT_UNION (decl) = 1; |
53065596 RK |
752 | else |
753 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
754 | break; | |
755 | ||
756 | case A_CONSTRUCTOR: | |
757 | if (TREE_CODE (decl) == FUNCTION_DECL | |
758 | && TREE_CODE (type) == FUNCTION_TYPE | |
759 | && decl_function_context (decl) == 0) | |
1a16a053 RK |
760 | { |
761 | DECL_STATIC_CONSTRUCTOR (decl) = 1; | |
762 | TREE_USED (decl) = 1; | |
763 | } | |
53065596 RK |
764 | else |
765 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
766 | break; | |
767 | ||
768 | case A_DESTRUCTOR: | |
769 | if (TREE_CODE (decl) == FUNCTION_DECL | |
770 | && TREE_CODE (type) == FUNCTION_TYPE | |
771 | && decl_function_context (decl) == 0) | |
1a16a053 RK |
772 | { |
773 | DECL_STATIC_DESTRUCTOR (decl) = 1; | |
774 | TREE_USED (decl) = 1; | |
775 | } | |
53065596 RK |
776 | else |
777 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
778 | break; | |
779 | ||
780 | case A_MODE: | |
781 | if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE) | |
782 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
783 | else | |
784 | { | |
785 | int j; | |
dff01034 | 786 | const char *p = IDENTIFIER_POINTER (TREE_VALUE (args)); |
53065596 RK |
787 | int len = strlen (p); |
788 | enum machine_mode mode = VOIDmode; | |
789 | tree typefm; | |
790 | ||
791 | if (len > 4 && p[0] == '_' && p[1] == '_' | |
792 | && p[len - 1] == '_' && p[len - 2] == '_') | |
793 | { | |
3438dff9 | 794 | char *newp = (char *) alloca (len - 1); |
53065596 RK |
795 | |
796 | strcpy (newp, &p[2]); | |
797 | newp[len - 4] = '\0'; | |
798 | p = newp; | |
799 | } | |
800 | ||
801 | /* Give this decl a type with the specified mode. | |
802 | First check for the special modes. */ | |
803 | if (! strcmp (p, "byte")) | |
804 | mode = byte_mode; | |
805 | else if (!strcmp (p, "word")) | |
806 | mode = word_mode; | |
807 | else if (! strcmp (p, "pointer")) | |
808 | mode = ptr_mode; | |
809 | else | |
810 | for (j = 0; j < NUM_MACHINE_MODES; j++) | |
811 | if (!strcmp (p, GET_MODE_NAME (j))) | |
812 | mode = (enum machine_mode) j; | |
813 | ||
814 | if (mode == VOIDmode) | |
815 | error ("unknown machine mode `%s'", p); | |
816 | else if (0 == (typefm = type_for_mode (mode, | |
817 | TREE_UNSIGNED (type)))) | |
818 | error ("no data type for mode `%s'", p); | |
819 | else | |
820 | { | |
b15ad712 JM |
821 | if (TYPE_PRECISION (typefm) > (TREE_UNSIGNED (type) |
822 | ? TYPE_PRECISION(uintmax_type_node) | |
823 | : TYPE_PRECISION(intmax_type_node)) | |
824 | && pedantic) | |
825 | pedwarn ("type with more precision than %s", | |
826 | TREE_UNSIGNED (type) ? "uintmax_t" : "intmax_t"); | |
53065596 | 827 | TREE_TYPE (decl) = type = typefm; |
06ceef4e | 828 | DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0; |
53065596 RK |
829 | layout_decl (decl, 0); |
830 | } | |
831 | } | |
832 | break; | |
833 | ||
834 | case A_SECTION: | |
5289b665 | 835 | #ifdef ASM_OUTPUT_SECTION_NAME |
53065596 RK |
836 | if ((TREE_CODE (decl) == FUNCTION_DECL |
837 | || TREE_CODE (decl) == VAR_DECL) | |
838 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
839 | { | |
b57062ca | 840 | if (TREE_CODE (decl) == VAR_DECL |
f4ca236c RK |
841 | && current_function_decl != NULL_TREE |
842 | && ! TREE_STATIC (decl)) | |
53065596 RK |
843 | error_with_decl (decl, |
844 | "section attribute cannot be specified for local variables"); | |
845 | /* The decl may have already been given a section attribute from | |
846 | a previous declaration. Ensure they match. */ | |
847 | else if (DECL_SECTION_NAME (decl) != NULL_TREE | |
848 | && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), | |
849 | TREE_STRING_POINTER (TREE_VALUE (args))) != 0) | |
850 | error_with_decl (node, | |
851 | "section of `%s' conflicts with previous declaration"); | |
852 | else | |
853 | DECL_SECTION_NAME (decl) = TREE_VALUE (args); | |
854 | } | |
855 | else | |
856 | error_with_decl (node, | |
5289b665 DE |
857 | "section attribute not allowed for `%s'"); |
858 | #else | |
53065596 RK |
859 | error_with_decl (node, |
860 | "section attributes are not supported for this target"); | |
5289b665 | 861 | #endif |
53065596 RK |
862 | break; |
863 | ||
864 | case A_ALIGNED: | |
6f38f669 | 865 | { |
53065596 | 866 | tree align_expr |
54630035 RK |
867 | = (args ? TREE_VALUE (args) |
868 | : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT)); | |
05bccae2 | 869 | int i; |
d125d268 | 870 | |
53065596 RK |
871 | /* Strip any NOPs of any kind. */ |
872 | while (TREE_CODE (align_expr) == NOP_EXPR | |
873 | || TREE_CODE (align_expr) == CONVERT_EXPR | |
874 | || TREE_CODE (align_expr) == NON_LVALUE_EXPR) | |
875 | align_expr = TREE_OPERAND (align_expr, 0); | |
b57062ca | 876 | |
53065596 RK |
877 | if (TREE_CODE (align_expr) != INTEGER_CST) |
878 | { | |
879 | error ("requested alignment is not a constant"); | |
880 | continue; | |
881 | } | |
882 | ||
05bccae2 | 883 | if ((i = tree_log2 (align_expr)) == -1) |
53065596 | 884 | error ("requested alignment is not a power of 2"); |
05bccae2 RK |
885 | else if (i > HOST_BITS_PER_INT - 2) |
886 | error ("requested alignment is too large"); | |
53065596 | 887 | else if (is_type) |
11cf4d18 | 888 | { |
aee3c6b0 JW |
889 | /* If we have a TYPE_DECL, then copy the type, so that we |
890 | don't accidentally modify a builtin type. See pushdecl. */ | |
891 | if (decl && TREE_TYPE (decl) != error_mark_node | |
892 | && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) | |
af80d489 | 893 | { |
aee3c6b0 JW |
894 | tree tt = TREE_TYPE (decl); |
895 | DECL_ORIGINAL_TYPE (decl) = tt; | |
896 | tt = build_type_copy (tt); | |
897 | TYPE_NAME (tt) = decl; | |
898 | TREE_USED (tt) = TREE_USED (decl); | |
899 | TREE_TYPE (decl) = tt; | |
900 | type = tt; | |
af80d489 | 901 | } |
aee3c6b0 JW |
902 | |
903 | TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT; | |
904 | TYPE_USER_ALIGN (type) = 1; | |
11cf4d18 | 905 | } |
53065596 RK |
906 | else if (TREE_CODE (decl) != VAR_DECL |
907 | && TREE_CODE (decl) != FIELD_DECL) | |
908 | error_with_decl (decl, | |
909 | "alignment may not be specified for `%s'"); | |
910 | else | |
11cf4d18 JJ |
911 | { |
912 | DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT; | |
913 | DECL_USER_ALIGN (decl) = 1; | |
914 | } | |
6f38f669 | 915 | } |
53065596 | 916 | break; |
6f38f669 | 917 | |
53065596 | 918 | case A_FORMAT: |
1312c143 JM |
919 | decl_handle_format_attribute (decl, args); |
920 | break; | |
4b8af8d9 | 921 | |
0161e8da | 922 | case A_FORMAT_ARG: |
1312c143 JM |
923 | decl_handle_format_arg_attribute (decl, args); |
924 | break; | |
0161e8da | 925 | |
4b8af8d9 JM |
926 | case A_WEAK: |
927 | declare_weak (decl); | |
928 | break; | |
929 | ||
930 | case A_ALIAS: | |
931 | if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) | |
5d9dd5a5 | 932 | || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl))) |
4b8af8d9 JM |
933 | error_with_decl (decl, |
934 | "`%s' defined both normally and as an alias"); | |
935 | else if (decl_function_context (decl) == 0) | |
936 | { | |
20045452 RH |
937 | tree id; |
938 | ||
939 | id = TREE_VALUE (args); | |
940 | if (TREE_CODE (id) != STRING_CST) | |
941 | { | |
942 | error ("alias arg not a string"); | |
943 | break; | |
944 | } | |
945 | id = get_identifier (TREE_STRING_POINTER (id)); | |
06a50fff ZW |
946 | /* This counts as a use of the object pointed to. */ |
947 | TREE_USED (id) = 1; | |
20045452 | 948 | |
4b8af8d9 JM |
949 | if (TREE_CODE (decl) == FUNCTION_DECL) |
950 | DECL_INITIAL (decl) = error_mark_node; | |
951 | else | |
952 | DECL_EXTERNAL (decl) = 0; | |
953 | assemble_alias (decl, id); | |
954 | } | |
955 | else | |
956 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
957 | break; | |
07417085 | 958 | |
7d384cc0 KR |
959 | case A_NO_CHECK_MEMORY_USAGE: |
960 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
961 | { | |
962 | error_with_decl (decl, | |
963 | "`%s' attribute applies only to functions", | |
964 | IDENTIFIER_POINTER (name)); | |
965 | } | |
966 | else if (DECL_INITIAL (decl)) | |
967 | { | |
968 | error_with_decl (decl, | |
969 | "can't set `%s' attribute after definition", | |
970 | IDENTIFIER_POINTER (name)); | |
971 | } | |
972 | else | |
973 | DECL_NO_CHECK_MEMORY_USAGE (decl) = 1; | |
974 | break; | |
975 | ||
07417085 KR |
976 | case A_NO_INSTRUMENT_FUNCTION: |
977 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
978 | { | |
979 | error_with_decl (decl, | |
980 | "`%s' attribute applies only to functions", | |
981 | IDENTIFIER_POINTER (name)); | |
982 | } | |
983 | else if (DECL_INITIAL (decl)) | |
984 | { | |
985 | error_with_decl (decl, | |
986 | "can't set `%s' attribute after definition", | |
987 | IDENTIFIER_POINTER (name)); | |
988 | } | |
989 | else | |
990 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
991 | break; | |
a157febd GK |
992 | |
993 | case A_NO_LIMIT_STACK: | |
994 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
995 | { | |
996 | error_with_decl (decl, | |
997 | "`%s' attribute applies only to functions", | |
998 | IDENTIFIER_POINTER (name)); | |
999 | } | |
1000 | else if (DECL_INITIAL (decl)) | |
1001 | { | |
1002 | error_with_decl (decl, | |
1003 | "can't set `%s' attribute after definition", | |
1004 | IDENTIFIER_POINTER (name)); | |
1005 | } | |
1006 | else | |
1007 | DECL_NO_LIMIT_STACK (decl) = 1; | |
1008 | break; | |
53065596 RK |
1009 | } |
1010 | } | |
b30f223b | 1011 | } |
800f4153 RK |
1012 | |
1013 | /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two | |
1014 | lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE). | |
1015 | ||
1016 | The head of the declspec list is stored in DECLSPECS. | |
1017 | The head of the attribute list is stored in PREFIX_ATTRIBUTES. | |
1018 | ||
1019 | Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of | |
1020 | the list elements. We drop the containing TREE_LIST nodes and link the | |
1021 | resulting attributes together the way decl_attributes expects them. */ | |
1022 | ||
1023 | void | |
1024 | split_specs_attrs (specs_attrs, declspecs, prefix_attributes) | |
1025 | tree specs_attrs; | |
1026 | tree *declspecs, *prefix_attributes; | |
1027 | { | |
1028 | tree t, s, a, next, specs, attrs; | |
1029 | ||
101e59f4 ML |
1030 | /* This can happen after an __extension__ in pedantic mode. */ |
1031 | if (specs_attrs != NULL_TREE | |
1032 | && TREE_CODE (specs_attrs) == INTEGER_CST) | |
1033 | { | |
1034 | *declspecs = NULL_TREE; | |
1035 | *prefix_attributes = NULL_TREE; | |
1036 | return; | |
1037 | } | |
1038 | ||
800f4153 RK |
1039 | /* This can happen in c++ (eg: decl: typespec initdecls ';'). */ |
1040 | if (specs_attrs != NULL_TREE | |
1041 | && TREE_CODE (specs_attrs) != TREE_LIST) | |
1042 | { | |
1043 | *declspecs = specs_attrs; | |
1044 | *prefix_attributes = NULL_TREE; | |
1045 | return; | |
1046 | } | |
1047 | ||
1048 | /* Remember to keep the lists in the same order, element-wise. */ | |
1049 | ||
1050 | specs = s = NULL_TREE; | |
1051 | attrs = a = NULL_TREE; | |
1052 | for (t = specs_attrs; t; t = next) | |
1053 | { | |
1054 | next = TREE_CHAIN (t); | |
1055 | /* Declspecs have a non-NULL TREE_VALUE. */ | |
1056 | if (TREE_VALUE (t) != NULL_TREE) | |
1057 | { | |
1058 | if (specs == NULL_TREE) | |
1059 | specs = s = t; | |
1060 | else | |
1061 | { | |
1062 | TREE_CHAIN (s) = t; | |
1063 | s = t; | |
1064 | } | |
1065 | } | |
1066 | else | |
1067 | { | |
1068 | if (attrs == NULL_TREE) | |
1069 | attrs = a = TREE_PURPOSE (t); | |
1070 | else | |
1071 | { | |
1072 | TREE_CHAIN (a) = TREE_PURPOSE (t); | |
1073 | a = TREE_PURPOSE (t); | |
1074 | } | |
1075 | /* More attrs can be linked here, move A to the end. */ | |
1076 | while (TREE_CHAIN (a) != NULL_TREE) | |
1077 | a = TREE_CHAIN (a); | |
1078 | } | |
1079 | } | |
1080 | ||
1081 | /* Terminate the lists. */ | |
1082 | if (s != NULL_TREE) | |
1083 | TREE_CHAIN (s) = NULL_TREE; | |
1084 | if (a != NULL_TREE) | |
1085 | TREE_CHAIN (a) = NULL_TREE; | |
1086 | ||
1087 | /* All done. */ | |
1088 | *declspecs = specs; | |
1089 | *prefix_attributes = attrs; | |
1090 | } | |
d9525bec BK |
1091 | |
1092 | /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes. | |
1093 | This function is used by the parser when a rule will accept attributes | |
1094 | in a particular position, but we don't want to support that just yet. | |
1095 | ||
1096 | A warning is issued for every ignored attribute. */ | |
1097 | ||
1098 | tree | |
1099 | strip_attrs (specs_attrs) | |
1100 | tree specs_attrs; | |
1101 | { | |
1102 | tree specs, attrs; | |
1103 | ||
1104 | split_specs_attrs (specs_attrs, &specs, &attrs); | |
1105 | ||
1106 | while (attrs) | |
1107 | { | |
1108 | warning ("`%s' attribute ignored", | |
1109 | IDENTIFIER_POINTER (TREE_PURPOSE (attrs))); | |
1110 | attrs = TREE_CHAIN (attrs); | |
1111 | } | |
1112 | ||
1113 | return specs; | |
1114 | } | |
b30f223b | 1115 | \f |
c70eaeaf | 1116 | static int is_valid_printf_arglist PARAMS ((tree)); |
f58e0b0c | 1117 | static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier)); |
c70eaeaf KG |
1118 | static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode, |
1119 | enum expand_modifier, int)); | |
18f988a0 KG |
1120 | static rtx c_expand_builtin_fprintf PARAMS ((tree, rtx, enum machine_mode, |
1121 | enum expand_modifier, int)); | |
1ccf251f | 1122 | \f |
d74154d5 RS |
1123 | /* Print a warning if a constant expression had overflow in folding. |
1124 | Invoke this function on every expression that the language | |
1125 | requires to be a constant expression. | |
1126 | Note the ANSI C standard says it is erroneous for a | |
1127 | constant expression to overflow. */ | |
96571883 BK |
1128 | |
1129 | void | |
1130 | constant_expression_warning (value) | |
1131 | tree value; | |
1132 | { | |
c05f751c RK |
1133 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
1134 | || TREE_CODE (value) == COMPLEX_CST) | |
1135 | && TREE_CONSTANT_OVERFLOW (value) && pedantic) | |
1136 | pedwarn ("overflow in constant expression"); | |
d74154d5 RS |
1137 | } |
1138 | ||
1139 | /* Print a warning if an expression had overflow in folding. | |
1140 | Invoke this function on every expression that | |
1141 | (1) appears in the source code, and | |
1142 | (2) might be a constant expression that overflowed, and | |
1143 | (3) is not already checked by convert_and_check; | |
1144 | however, do not invoke this function on operands of explicit casts. */ | |
1145 | ||
1146 | void | |
1147 | overflow_warning (value) | |
1148 | tree value; | |
1149 | { | |
c05f751c RK |
1150 | if ((TREE_CODE (value) == INTEGER_CST |
1151 | || (TREE_CODE (value) == COMPLEX_CST | |
1152 | && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)) | |
1153 | && TREE_OVERFLOW (value)) | |
d74154d5 | 1154 | { |
7193bce2 | 1155 | TREE_OVERFLOW (value) = 0; |
e78a3b42 RK |
1156 | if (skip_evaluation == 0) |
1157 | warning ("integer overflow in expression"); | |
d74154d5 | 1158 | } |
c05f751c RK |
1159 | else if ((TREE_CODE (value) == REAL_CST |
1160 | || (TREE_CODE (value) == COMPLEX_CST | |
1161 | && TREE_CODE (TREE_REALPART (value)) == REAL_CST)) | |
1162 | && TREE_OVERFLOW (value)) | |
1163 | { | |
1164 | TREE_OVERFLOW (value) = 0; | |
e78a3b42 RK |
1165 | if (skip_evaluation == 0) |
1166 | warning ("floating point overflow in expression"); | |
c05f751c | 1167 | } |
d74154d5 RS |
1168 | } |
1169 | ||
1170 | /* Print a warning if a large constant is truncated to unsigned, | |
1171 | or if -Wconversion is used and a constant < 0 is converted to unsigned. | |
1172 | Invoke this function on every expression that might be implicitly | |
1173 | converted to an unsigned type. */ | |
1174 | ||
1175 | void | |
1176 | unsigned_conversion_warning (result, operand) | |
1177 | tree result, operand; | |
1178 | { | |
1179 | if (TREE_CODE (operand) == INTEGER_CST | |
1180 | && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE | |
1181 | && TREE_UNSIGNED (TREE_TYPE (result)) | |
e78a3b42 | 1182 | && skip_evaluation == 0 |
d74154d5 RS |
1183 | && !int_fits_type_p (operand, TREE_TYPE (result))) |
1184 | { | |
1185 | if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result)))) | |
1186 | /* This detects cases like converting -129 or 256 to unsigned char. */ | |
90c939d4 | 1187 | warning ("large integer implicitly truncated to unsigned type"); |
d74154d5 | 1188 | else if (warn_conversion) |
90c939d4 | 1189 | warning ("negative integer implicitly converted to unsigned type"); |
d74154d5 RS |
1190 | } |
1191 | } | |
1192 | ||
d02b54f6 JJ |
1193 | /* Nonzero if constant C has a value that is permissible |
1194 | for type TYPE (an INTEGER_TYPE). */ | |
1195 | ||
1196 | static int | |
1197 | constant_fits_type_p (c, type) | |
1198 | tree c, type; | |
1199 | { | |
1200 | if (TREE_CODE (c) == INTEGER_CST) | |
1201 | return int_fits_type_p (c, type); | |
1202 | ||
1203 | c = convert (type, c); | |
1204 | return !TREE_OVERFLOW (c); | |
1205 | } | |
1206 | ||
d74154d5 RS |
1207 | /* Convert EXPR to TYPE, warning about conversion problems with constants. |
1208 | Invoke this function on every expression that is converted implicitly, | |
1209 | i.e. because of language rules and not because of an explicit cast. */ | |
1210 | ||
1211 | tree | |
1212 | convert_and_check (type, expr) | |
1213 | tree type, expr; | |
1214 | { | |
1215 | tree t = convert (type, expr); | |
1216 | if (TREE_CODE (t) == INTEGER_CST) | |
1217 | { | |
7193bce2 | 1218 | if (TREE_OVERFLOW (t)) |
d74154d5 | 1219 | { |
7193bce2 PE |
1220 | TREE_OVERFLOW (t) = 0; |
1221 | ||
868fc750 RK |
1222 | /* Do not diagnose overflow in a constant expression merely |
1223 | because a conversion overflowed. */ | |
1224 | TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr); | |
1225 | ||
7193bce2 PE |
1226 | /* No warning for converting 0x80000000 to int. */ |
1227 | if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr)) | |
1228 | && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
1229 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr)))) | |
22ba338b RS |
1230 | /* If EXPR fits in the unsigned version of TYPE, |
1231 | don't warn unless pedantic. */ | |
e78a3b42 RK |
1232 | if ((pedantic |
1233 | || TREE_UNSIGNED (type) | |
d02b54f6 | 1234 | || ! constant_fits_type_p (expr, unsigned_type (type))) |
e78a3b42 | 1235 | && skip_evaluation == 0) |
bb72a084 | 1236 | warning ("overflow in implicit constant conversion"); |
d74154d5 RS |
1237 | } |
1238 | else | |
1239 | unsigned_conversion_warning (t, expr); | |
1240 | } | |
1241 | return t; | |
96571883 BK |
1242 | } |
1243 | \f | |
235cfbc4 BS |
1244 | /* A node in a list that describes references to variables (EXPR), which are |
1245 | either read accesses if WRITER is zero, or write accesses, in which case | |
1246 | WRITER is the parent of EXPR. */ | |
1247 | struct tlist | |
1248 | { | |
1249 | struct tlist *next; | |
1250 | tree expr, writer; | |
1251 | }; | |
1252 | ||
1253 | /* Used to implement a cache the results of a call to verify_tree. We only | |
1254 | use this for SAVE_EXPRs. */ | |
1255 | struct tlist_cache | |
1256 | { | |
1257 | struct tlist_cache *next; | |
1258 | struct tlist *cache_before_sp; | |
1259 | struct tlist *cache_after_sp; | |
1260 | tree expr; | |
2683ed8d BS |
1261 | }; |
1262 | ||
235cfbc4 BS |
1263 | /* Obstack to use when allocating tlist structures, and corresponding |
1264 | firstobj. */ | |
1265 | static struct obstack tlist_obstack; | |
1266 | static char *tlist_firstobj = 0; | |
1267 | ||
1268 | /* Keep track of the identifiers we've warned about, so we can avoid duplicate | |
1269 | warnings. */ | |
1270 | static struct tlist *warned_ids; | |
1271 | /* SAVE_EXPRs need special treatment. We process them only once and then | |
1272 | cache the results. */ | |
1273 | static struct tlist_cache *save_expr_cache; | |
1274 | ||
1275 | static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int)); | |
1276 | static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int)); | |
1277 | static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree)); | |
1278 | static int warning_candidate_p PARAMS ((tree)); | |
1279 | static void warn_for_collisions PARAMS ((struct tlist *)); | |
1280 | static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int)); | |
1281 | static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree)); | |
2683ed8d BS |
1282 | static void verify_sequence_points PARAMS ((tree)); |
1283 | ||
235cfbc4 BS |
1284 | /* Create a new struct tlist and fill in its fields. */ |
1285 | static struct tlist * | |
1286 | new_tlist (next, t, writer) | |
1287 | struct tlist *next; | |
1288 | tree t; | |
1289 | tree writer; | |
1290 | { | |
1291 | struct tlist *l; | |
1292 | l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l); | |
1293 | l->next = next; | |
1294 | l->expr = t; | |
1295 | l->writer = writer; | |
1296 | return l; | |
1297 | } | |
1298 | ||
1299 | /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER | |
1300 | is nonnull, we ignore any node we find which has a writer equal to it. */ | |
1301 | ||
1302 | static void | |
1303 | add_tlist (to, add, exclude_writer, copy) | |
1304 | struct tlist **to; | |
1305 | struct tlist *add; | |
1306 | tree exclude_writer; | |
1307 | int copy; | |
1308 | { | |
1309 | while (add) | |
1310 | { | |
1311 | struct tlist *next = add->next; | |
1312 | if (! copy) | |
1313 | add->next = *to; | |
1314 | if (! exclude_writer || add->writer != exclude_writer) | |
1315 | *to = copy ? new_tlist (*to, add->expr, add->writer) : add; | |
1316 | add = next; | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | /* Merge the nodes of ADD into TO. This merging process is done so that for | |
1321 | each variable that already exists in TO, no new node is added; however if | |
1322 | there is a write access recorded in ADD, and an occurrence on TO is only | |
1323 | a read access, then the occurrence in TO will be modified to record the | |
1324 | write. */ | |
2683ed8d BS |
1325 | |
1326 | static void | |
235cfbc4 BS |
1327 | merge_tlist (to, add, copy) |
1328 | struct tlist **to; | |
1329 | struct tlist *add; | |
1330 | int copy; | |
1331 | { | |
1332 | struct tlist **end = to; | |
1333 | ||
1334 | while (*end) | |
1335 | end = &(*end)->next; | |
1336 | ||
1337 | while (add) | |
1338 | { | |
1339 | int found = 0; | |
1340 | struct tlist *tmp2; | |
1341 | struct tlist *next = add->next; | |
1342 | ||
1343 | for (tmp2 = *to; tmp2; tmp2 = tmp2->next) | |
1344 | if (tmp2->expr == add->expr) | |
1345 | { | |
1346 | found = 1; | |
1347 | if (! tmp2->writer) | |
1348 | tmp2->writer = add->writer; | |
1349 | } | |
1350 | if (! found) | |
1351 | { | |
1352 | *end = copy ? add : new_tlist (NULL, add->expr, add->writer); | |
1353 | end = &(*end)->next; | |
1354 | *end = 0; | |
1355 | } | |
1356 | add = next; | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable | |
1361 | references in list LIST conflict with it, excluding reads if ONLY writers | |
1362 | is nonzero. */ | |
1363 | ||
1364 | static void | |
1365 | warn_for_collisions_1 (written, writer, list, only_writes) | |
1366 | tree written, writer; | |
1367 | struct tlist *list; | |
1368 | int only_writes; | |
1369 | { | |
1370 | struct tlist *tmp; | |
1371 | ||
1372 | /* Avoid duplicate warnings. */ | |
1373 | for (tmp = warned_ids; tmp; tmp = tmp->next) | |
1374 | if (tmp->expr == written) | |
1375 | return; | |
1376 | ||
1377 | while (list) | |
1378 | { | |
1379 | if (list->expr == written | |
1380 | && list->writer != writer | |
1381 | && (! only_writes || list->writer)) | |
1382 | { | |
1383 | warned_ids = new_tlist (warned_ids, written, NULL_TREE); | |
1384 | warning ("operation on `%s' may be undefined", | |
1385 | IDENTIFIER_POINTER (DECL_NAME (list->expr))); | |
1386 | } | |
1387 | list = list->next; | |
1388 | } | |
1389 | } | |
1390 | ||
1391 | /* Given a list LIST of references to variables, find whether any of these | |
1392 | can cause conflicts due to missing sequence points. */ | |
1393 | ||
1394 | static void | |
1395 | warn_for_collisions (list) | |
1396 | struct tlist *list; | |
1397 | { | |
1398 | struct tlist *tmp; | |
1399 | ||
1400 | for (tmp = list; tmp; tmp = tmp->next) | |
1401 | { | |
1402 | if (tmp->writer) | |
1403 | warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); | |
1404 | } | |
1405 | } | |
1406 | ||
1407 | /* Return nonzero if X is a tree that can be verified by the sequence poitn | |
1408 | warnings. */ | |
1409 | static int | |
1410 | warning_candidate_p (x) | |
2683ed8d | 1411 | tree x; |
2683ed8d | 1412 | { |
235cfbc4 BS |
1413 | return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL; |
1414 | } | |
2683ed8d | 1415 | |
235cfbc4 BS |
1416 | /* Walk the tree X, and record accesses to variables. If X is written by the |
1417 | parent tree, WRITER is the parent. | |
1418 | We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this | |
1419 | expression or its only operand forces a sequence point, then everything up | |
1420 | to the sequence point is stored in PBEFORE_SP. Everything else gets stored | |
1421 | in PNO_SP. | |
1422 | Once we return, we will have emitted warnings if any subexpression before | |
1423 | such a sequence point could be undefined. On a higher level, however, the | |
1424 | sequence point may not be relevant, and we'll merge the two lists. | |
1425 | ||
1426 | Example: (b++, a) + b; | |
1427 | The call that processes the COMPOUND_EXPR will store the increment of B | |
1428 | in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that | |
1429 | processes the PLUS_EXPR will need to merge the two lists so that | |
1430 | eventually, all accesses end up on the same list (and we'll warn about the | |
1431 | unordered subexpressions b++ and b. | |
1432 | ||
1433 | A note on merging. If we modify the former example so that our expression | |
1434 | becomes | |
1435 | (b++, b) + a | |
1436 | care must be taken not simply to add all three expressions into the final | |
1437 | PNO_SP list. The function merge_tlist takes care of that by merging the | |
1438 | before-SP list of the COMPOUND_EXPR into its after-SP list in a special | |
1439 | way, so that no more than one access to B is recorded. */ | |
2683ed8d | 1440 | |
235cfbc4 BS |
1441 | static void |
1442 | verify_tree (x, pbefore_sp, pno_sp, writer) | |
1443 | tree x; | |
1444 | struct tlist **pbefore_sp, **pno_sp; | |
1445 | tree writer; | |
1446 | { | |
1447 | struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; | |
1448 | enum tree_code code; | |
1449 | char class; | |
2683ed8d | 1450 | |
235cfbc4 BS |
1451 | restart: |
1452 | code = TREE_CODE (x); | |
1453 | class = TREE_CODE_CLASS (code); | |
2683ed8d | 1454 | |
235cfbc4 | 1455 | if (warning_candidate_p (x)) |
2683ed8d | 1456 | { |
235cfbc4 BS |
1457 | *pno_sp = new_tlist (*pno_sp, x, writer); |
1458 | return; | |
1459 | } | |
1460 | ||
1461 | switch (code) | |
1462 | { | |
52a84e42 BS |
1463 | case CONSTRUCTOR: |
1464 | return; | |
1465 | ||
235cfbc4 BS |
1466 | case COMPOUND_EXPR: |
1467 | case TRUTH_ANDIF_EXPR: | |
1468 | case TRUTH_ORIF_EXPR: | |
1469 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
1470 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
1471 | warn_for_collisions (tmp_nosp); | |
1472 | merge_tlist (pbefore_sp, tmp_before, 0); | |
1473 | merge_tlist (pbefore_sp, tmp_nosp, 0); | |
1474 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); | |
1475 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
1476 | return; | |
1477 | ||
1478 | case COND_EXPR: | |
1479 | tmp_before = tmp_list2 = 0; | |
1480 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); | |
1481 | warn_for_collisions (tmp_list2); | |
1482 | merge_tlist (pbefore_sp, tmp_before, 0); | |
1483 | merge_tlist (pbefore_sp, tmp_list2, 1); | |
1484 | ||
1485 | tmp_list3 = tmp_nosp = 0; | |
1486 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); | |
1487 | warn_for_collisions (tmp_nosp); | |
1488 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
1489 | ||
1490 | tmp_list3 = tmp_list2 = 0; | |
1491 | verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); | |
1492 | warn_for_collisions (tmp_list2); | |
1493 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
1494 | /* Rather than add both tmp_nosp and tmp_list2, we have to merge the | |
1495 | two first, to avoid warning for (a ? b++ : b++). */ | |
1496 | merge_tlist (&tmp_nosp, tmp_list2, 0); | |
1497 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
1498 | return; | |
1499 | ||
2683ed8d BS |
1500 | case PREDECREMENT_EXPR: |
1501 | case PREINCREMENT_EXPR: | |
1502 | case POSTDECREMENT_EXPR: | |
1503 | case POSTINCREMENT_EXPR: | |
235cfbc4 BS |
1504 | verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); |
1505 | return; | |
1506 | ||
1507 | case MODIFY_EXPR: | |
1508 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
1509 | verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); | |
1510 | verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); | |
1511 | /* Expressions inside the LHS are not ordered wrt. the sequence points | |
1512 | in the RHS. Example: | |
1513 | *a = (a++, 2) | |
1514 | Despite the fact that the modification of "a" is in the before_sp | |
1515 | list (tmp_before), it conflicts with the use of "a" in the LHS. | |
1516 | We can handle this by adding the contents of tmp_list3 | |
1517 | to those of tmp_before, and redoing the collision warnings for that | |
1518 | list. */ | |
1519 | add_tlist (&tmp_before, tmp_list3, x, 1); | |
1520 | warn_for_collisions (tmp_before); | |
1521 | /* Exclude the LHS itself here; we first have to merge it into the | |
1522 | tmp_nosp list. This is done to avoid warning for "a = a"; if we | |
1523 | didn't exclude the LHS, we'd get it twice, once as a read and once | |
1524 | as a write. */ | |
1525 | add_tlist (pno_sp, tmp_list3, x, 0); | |
1526 | warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); | |
1527 | ||
1528 | merge_tlist (pbefore_sp, tmp_before, 0); | |
1529 | if (warning_candidate_p (TREE_OPERAND (x, 0))) | |
1530 | merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); | |
1531 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); | |
1532 | return; | |
2683ed8d BS |
1533 | |
1534 | case CALL_EXPR: | |
235cfbc4 BS |
1535 | /* We need to warn about conflicts among arguments and conflicts between |
1536 | args and the function address. Side effects of the function address, | |
1537 | however, are not ordered by the sequence point of the call. */ | |
1538 | tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0; | |
1539 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
1540 | if (TREE_OPERAND (x, 1)) | |
1541 | verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE); | |
1542 | merge_tlist (&tmp_list3, tmp_list2, 0); | |
1543 | add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); | |
1544 | add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); | |
1545 | warn_for_collisions (tmp_before); | |
1546 | add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); | |
1547 | return; | |
2683ed8d BS |
1548 | |
1549 | case TREE_LIST: | |
1550 | /* Scan all the list, e.g. indices of multi dimensional array. */ | |
1551 | while (x) | |
1552 | { | |
235cfbc4 BS |
1553 | tmp_before = tmp_nosp = 0; |
1554 | verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
1555 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
1556 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
2683ed8d BS |
1557 | x = TREE_CHAIN (x); |
1558 | } | |
235cfbc4 | 1559 | return; |
2683ed8d | 1560 | |
235cfbc4 BS |
1561 | case SAVE_EXPR: |
1562 | { | |
1563 | struct tlist_cache *t; | |
1564 | for (t = save_expr_cache; t; t = t->next) | |
1565 | if (t->expr == x) | |
1566 | break; | |
2683ed8d | 1567 | |
235cfbc4 | 1568 | if (! t) |
2683ed8d | 1569 | { |
235cfbc4 BS |
1570 | t = (struct tlist_cache *) obstack_alloc (&tlist_obstack, |
1571 | sizeof *t); | |
1572 | t->next = save_expr_cache; | |
1573 | t->expr = x; | |
1574 | save_expr_cache = t; | |
1575 | ||
1576 | tmp_before = tmp_nosp = 0; | |
1577 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
1578 | warn_for_collisions (tmp_nosp); | |
1579 | ||
1580 | tmp_list3 = 0; | |
1581 | while (tmp_nosp) | |
1582 | { | |
1583 | struct tlist *t = tmp_nosp; | |
1584 | tmp_nosp = t->next; | |
1585 | merge_tlist (&tmp_list3, t, 0); | |
1586 | } | |
1587 | t->cache_before_sp = tmp_before; | |
1588 | t->cache_after_sp = tmp_list3; | |
2683ed8d | 1589 | } |
235cfbc4 BS |
1590 | merge_tlist (pbefore_sp, t->cache_before_sp, 1); |
1591 | add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); | |
1592 | return; | |
1593 | } | |
1594 | default: | |
2683ed8d BS |
1595 | break; |
1596 | } | |
2683ed8d | 1597 | |
235cfbc4 | 1598 | if (class == '1') |
2683ed8d | 1599 | { |
235cfbc4 BS |
1600 | if (first_rtl_op (code) == 0) |
1601 | return; | |
1602 | x = TREE_OPERAND (x, 0); | |
1603 | writer = 0; | |
1604 | goto restart; | |
2683ed8d BS |
1605 | } |
1606 | ||
235cfbc4 | 1607 | switch (class) |
2683ed8d | 1608 | { |
235cfbc4 BS |
1609 | case 'r': |
1610 | case '<': | |
1611 | case '2': | |
1612 | case 'b': | |
1613 | case 'e': | |
1614 | case 's': | |
1615 | case 'x': | |
1616 | { | |
1617 | int lp; | |
1618 | int max = first_rtl_op (TREE_CODE (x)); | |
1619 | for (lp = 0; lp < max; lp++) | |
1620 | { | |
1621 | tmp_before = tmp_nosp = 0; | |
1622 | verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE); | |
1623 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
1624 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
1625 | } | |
1626 | break; | |
1627 | } | |
2683ed8d | 1628 | } |
2683ed8d BS |
1629 | } |
1630 | ||
1631 | /* Try to warn for undefined behaviour in EXPR due to missing sequence | |
1632 | points. */ | |
1633 | ||
1634 | static void | |
1635 | verify_sequence_points (expr) | |
1636 | tree expr; | |
1637 | { | |
235cfbc4 | 1638 | struct tlist *before_sp = 0, *after_sp = 0; |
2683ed8d | 1639 | |
235cfbc4 BS |
1640 | warned_ids = 0; |
1641 | save_expr_cache = 0; | |
1642 | if (tlist_firstobj == 0) | |
2683ed8d | 1643 | { |
235cfbc4 BS |
1644 | gcc_obstack_init (&tlist_obstack); |
1645 | tlist_firstobj = obstack_alloc (&tlist_obstack, 0); | |
2683ed8d BS |
1646 | } |
1647 | ||
235cfbc4 BS |
1648 | verify_tree (expr, &before_sp, &after_sp, 0); |
1649 | warn_for_collisions (after_sp); | |
1650 | obstack_free (&tlist_obstack, tlist_firstobj); | |
2683ed8d BS |
1651 | } |
1652 | ||
64094f6a | 1653 | tree |
b30f223b RS |
1654 | c_expand_expr_stmt (expr) |
1655 | tree expr; | |
1656 | { | |
1657 | /* Do default conversion if safe and possibly important, | |
1658 | in case within ({...}). */ | |
1659 | if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr)) | |
1660 | || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE) | |
1661 | expr = default_conversion (expr); | |
1662 | ||
2683ed8d BS |
1663 | if (warn_sequence_point) |
1664 | verify_sequence_points (expr); | |
1665 | ||
b30f223b | 1666 | if (TREE_TYPE (expr) != error_mark_node |
b8de2d02 | 1667 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) |
b30f223b RS |
1668 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) |
1669 | error ("expression statement has incomplete type"); | |
1670 | ||
8f17b5c5 | 1671 | last_expr_type = TREE_TYPE (expr); |
64094f6a | 1672 | return add_stmt (build_stmt (EXPR_STMT, expr)); |
b30f223b RS |
1673 | } |
1674 | \f | |
1675 | /* Validate the expression after `case' and apply default promotions. */ | |
1676 | ||
1677 | tree | |
1678 | check_case_value (value) | |
1679 | tree value; | |
1680 | { | |
1681 | if (value == NULL_TREE) | |
1682 | return value; | |
1683 | ||
1684 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
8493738b | 1685 | STRIP_TYPE_NOPS (value); |
56cb9733 MM |
1686 | /* In C++, the following is allowed: |
1687 | ||
1688 | const int i = 3; | |
1689 | switch (...) { case i: ... } | |
1690 | ||
1691 | So, we try to reduce the VALUE to a constant that way. */ | |
1692 | if (c_language == clk_cplusplus) | |
1693 | { | |
1694 | value = decl_constant_value (value); | |
1695 | STRIP_TYPE_NOPS (value); | |
1696 | value = fold (value); | |
1697 | } | |
b30f223b RS |
1698 | |
1699 | if (TREE_CODE (value) != INTEGER_CST | |
1700 | && value != error_mark_node) | |
1701 | { | |
1702 | error ("case label does not reduce to an integer constant"); | |
1703 | value = error_mark_node; | |
1704 | } | |
1705 | else | |
1706 | /* Promote char or short to int. */ | |
1707 | value = default_conversion (value); | |
1708 | ||
bc690db1 RS |
1709 | constant_expression_warning (value); |
1710 | ||
b30f223b RS |
1711 | return value; |
1712 | } | |
1713 | \f | |
1714 | /* Return an integer type with BITS bits of precision, | |
1715 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
1716 | ||
1717 | tree | |
1718 | type_for_size (bits, unsignedp) | |
1719 | unsigned bits; | |
1720 | int unsignedp; | |
1721 | { | |
a311b52c JM |
1722 | if (bits == TYPE_PRECISION (integer_type_node)) |
1723 | return unsignedp ? unsigned_type_node : integer_type_node; | |
1724 | ||
3fc7e390 | 1725 | if (bits == TYPE_PRECISION (signed_char_type_node)) |
b30f223b RS |
1726 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
1727 | ||
3fc7e390 | 1728 | if (bits == TYPE_PRECISION (short_integer_type_node)) |
b30f223b RS |
1729 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
1730 | ||
3fc7e390 | 1731 | if (bits == TYPE_PRECISION (long_integer_type_node)) |
b30f223b RS |
1732 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
1733 | ||
3fc7e390 | 1734 | if (bits == TYPE_PRECISION (long_long_integer_type_node)) |
b30f223b RS |
1735 | return (unsignedp ? long_long_unsigned_type_node |
1736 | : long_long_integer_type_node); | |
1737 | ||
835f9b4d GRK |
1738 | if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) |
1739 | return (unsignedp ? widest_unsigned_literal_type_node | |
1740 | : widest_integer_literal_type_node); | |
1741 | ||
3fc7e390 RS |
1742 | if (bits <= TYPE_PRECISION (intQI_type_node)) |
1743 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
1744 | ||
1745 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
1746 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
1747 | ||
1748 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
1749 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
1750 | ||
1751 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
1752 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
1753 | ||
b30f223b RS |
1754 | return 0; |
1755 | } | |
1756 | ||
1757 | /* Return a data type that has machine mode MODE. | |
1758 | If the mode is an integer, | |
1759 | then UNSIGNEDP selects between signed and unsigned types. */ | |
1760 | ||
1761 | tree | |
1762 | type_for_mode (mode, unsignedp) | |
1763 | enum machine_mode mode; | |
1764 | int unsignedp; | |
1765 | { | |
a311b52c JM |
1766 | if (mode == TYPE_MODE (integer_type_node)) |
1767 | return unsignedp ? unsigned_type_node : integer_type_node; | |
1768 | ||
b30f223b RS |
1769 | if (mode == TYPE_MODE (signed_char_type_node)) |
1770 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
1771 | ||
1772 | if (mode == TYPE_MODE (short_integer_type_node)) | |
1773 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
1774 | ||
b30f223b RS |
1775 | if (mode == TYPE_MODE (long_integer_type_node)) |
1776 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
1777 | ||
1778 | if (mode == TYPE_MODE (long_long_integer_type_node)) | |
1779 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
1780 | ||
835f9b4d | 1781 | if (mode == TYPE_MODE (widest_integer_literal_type_node)) |
d125d268 | 1782 | return unsignedp ? widest_unsigned_literal_type_node |
835f9b4d GRK |
1783 | : widest_integer_literal_type_node; |
1784 | ||
3fc7e390 RS |
1785 | if (mode == TYPE_MODE (intQI_type_node)) |
1786 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
1787 | ||
1788 | if (mode == TYPE_MODE (intHI_type_node)) | |
1789 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
1790 | ||
1791 | if (mode == TYPE_MODE (intSI_type_node)) | |
1792 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
1793 | ||
1794 | if (mode == TYPE_MODE (intDI_type_node)) | |
1795 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
1796 | ||
21a9616b | 1797 | #if HOST_BITS_PER_WIDE_INT >= 64 |
a6d7e156 JL |
1798 | if (mode == TYPE_MODE (intTI_type_node)) |
1799 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
21a9616b | 1800 | #endif |
a6d7e156 | 1801 | |
b30f223b RS |
1802 | if (mode == TYPE_MODE (float_type_node)) |
1803 | return float_type_node; | |
1804 | ||
1805 | if (mode == TYPE_MODE (double_type_node)) | |
1806 | return double_type_node; | |
1807 | ||
1808 | if (mode == TYPE_MODE (long_double_type_node)) | |
1809 | return long_double_type_node; | |
1810 | ||
1811 | if (mode == TYPE_MODE (build_pointer_type (char_type_node))) | |
1812 | return build_pointer_type (char_type_node); | |
1813 | ||
1814 | if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) | |
1815 | return build_pointer_type (integer_type_node); | |
1816 | ||
4061f623 BS |
1817 | #ifdef VECTOR_MODE_SUPPORTED_P |
1818 | if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode)) | |
1819 | return V4SF_type_node; | |
1820 | if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode)) | |
1821 | return V4SI_type_node; | |
1822 | if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode)) | |
1823 | return V2SI_type_node; | |
1824 | if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode)) | |
1825 | return V4HI_type_node; | |
1826 | if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode)) | |
1827 | return V8QI_type_node; | |
1828 | #endif | |
1829 | ||
b30f223b RS |
1830 | return 0; |
1831 | } | |
693a6128 GRK |
1832 | |
1833 | /* Return an unsigned type the same as TYPE in other respects. */ | |
1834 | tree | |
1835 | unsigned_type (type) | |
1836 | tree type; | |
1837 | { | |
1838 | tree type1 = TYPE_MAIN_VARIANT (type); | |
1839 | if (type1 == signed_char_type_node || type1 == char_type_node) | |
1840 | return unsigned_char_type_node; | |
1841 | if (type1 == integer_type_node) | |
1842 | return unsigned_type_node; | |
1843 | if (type1 == short_integer_type_node) | |
1844 | return short_unsigned_type_node; | |
1845 | if (type1 == long_integer_type_node) | |
1846 | return long_unsigned_type_node; | |
1847 | if (type1 == long_long_integer_type_node) | |
1848 | return long_long_unsigned_type_node; | |
1849 | if (type1 == widest_integer_literal_type_node) | |
1850 | return widest_unsigned_literal_type_node; | |
1851 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
1852 | if (type1 == intTI_type_node) | |
1853 | return unsigned_intTI_type_node; | |
1854 | #endif | |
1855 | if (type1 == intDI_type_node) | |
1856 | return unsigned_intDI_type_node; | |
1857 | if (type1 == intSI_type_node) | |
1858 | return unsigned_intSI_type_node; | |
1859 | if (type1 == intHI_type_node) | |
1860 | return unsigned_intHI_type_node; | |
1861 | if (type1 == intQI_type_node) | |
1862 | return unsigned_intQI_type_node; | |
1863 | ||
1864 | return signed_or_unsigned_type (1, type); | |
1865 | } | |
1866 | ||
1867 | /* Return a signed type the same as TYPE in other respects. */ | |
1868 | ||
1869 | tree | |
1870 | signed_type (type) | |
1871 | tree type; | |
1872 | { | |
1873 | tree type1 = TYPE_MAIN_VARIANT (type); | |
1874 | if (type1 == unsigned_char_type_node || type1 == char_type_node) | |
1875 | return signed_char_type_node; | |
1876 | if (type1 == unsigned_type_node) | |
1877 | return integer_type_node; | |
1878 | if (type1 == short_unsigned_type_node) | |
1879 | return short_integer_type_node; | |
1880 | if (type1 == long_unsigned_type_node) | |
1881 | return long_integer_type_node; | |
1882 | if (type1 == long_long_unsigned_type_node) | |
1883 | return long_long_integer_type_node; | |
1884 | if (type1 == widest_unsigned_literal_type_node) | |
1885 | return widest_integer_literal_type_node; | |
1886 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
1887 | if (type1 == unsigned_intTI_type_node) | |
1888 | return intTI_type_node; | |
1889 | #endif | |
1890 | if (type1 == unsigned_intDI_type_node) | |
1891 | return intDI_type_node; | |
1892 | if (type1 == unsigned_intSI_type_node) | |
1893 | return intSI_type_node; | |
1894 | if (type1 == unsigned_intHI_type_node) | |
1895 | return intHI_type_node; | |
1896 | if (type1 == unsigned_intQI_type_node) | |
1897 | return intQI_type_node; | |
1898 | ||
1899 | return signed_or_unsigned_type (0, type); | |
1900 | } | |
1901 | ||
1902 | /* Return a type the same as TYPE except unsigned or | |
1903 | signed according to UNSIGNEDP. */ | |
1904 | ||
1905 | tree | |
1906 | signed_or_unsigned_type (unsignedp, type) | |
1907 | int unsignedp; | |
1908 | tree type; | |
1909 | { | |
1910 | if (! INTEGRAL_TYPE_P (type) | |
1911 | || TREE_UNSIGNED (type) == unsignedp) | |
1912 | return type; | |
1913 | ||
1914 | if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)) | |
1915 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
d125d268 | 1916 | if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
693a6128 | 1917 | return unsignedp ? unsigned_type_node : integer_type_node; |
d125d268 | 1918 | if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node)) |
693a6128 | 1919 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
d125d268 | 1920 | if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node)) |
693a6128 | 1921 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
d125d268 | 1922 | if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node)) |
693a6128 GRK |
1923 | return (unsignedp ? long_long_unsigned_type_node |
1924 | : long_long_integer_type_node); | |
d125d268 | 1925 | if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node)) |
693a6128 GRK |
1926 | return (unsignedp ? widest_unsigned_literal_type_node |
1927 | : widest_integer_literal_type_node); | |
1928 | return type; | |
1929 | } | |
b30f223b | 1930 | \f |
6acfe908 JM |
1931 | /* Return the minimum number of bits needed to represent VALUE in a |
1932 | signed or unsigned type, UNSIGNEDP says which. */ | |
1933 | ||
05bccae2 | 1934 | unsigned int |
6acfe908 JM |
1935 | min_precision (value, unsignedp) |
1936 | tree value; | |
1937 | int unsignedp; | |
1938 | { | |
1939 | int log; | |
1940 | ||
1941 | /* If the value is negative, compute its negative minus 1. The latter | |
1942 | adjustment is because the absolute value of the largest negative value | |
1943 | is one larger than the largest positive value. This is equivalent to | |
1944 | a bit-wise negation, so use that operation instead. */ | |
1945 | ||
1946 | if (tree_int_cst_sgn (value) < 0) | |
1947 | value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value)); | |
1948 | ||
1949 | /* Return the number of bits needed, taking into account the fact | |
1950 | that we need one more bit for a signed than unsigned type. */ | |
1951 | ||
1952 | if (integer_zerop (value)) | |
1953 | log = 0; | |
6acfe908 | 1954 | else |
05bccae2 | 1955 | log = tree_floor_log2 (value); |
6acfe908 JM |
1956 | |
1957 | return log + 1 + ! unsignedp; | |
1958 | } | |
1959 | \f | |
b30f223b RS |
1960 | /* Print an error message for invalid operands to arith operation CODE. |
1961 | NOP_EXPR is used as a special case (see truthvalue_conversion). */ | |
1962 | ||
1963 | void | |
1964 | binary_op_error (code) | |
1965 | enum tree_code code; | |
1966 | { | |
dff01034 | 1967 | register const char *opname; |
89c78d7d | 1968 | |
b30f223b RS |
1969 | switch (code) |
1970 | { | |
1971 | case NOP_EXPR: | |
1972 | error ("invalid truth-value expression"); | |
1973 | return; | |
1974 | ||
1975 | case PLUS_EXPR: | |
1976 | opname = "+"; break; | |
1977 | case MINUS_EXPR: | |
1978 | opname = "-"; break; | |
1979 | case MULT_EXPR: | |
1980 | opname = "*"; break; | |
1981 | case MAX_EXPR: | |
1982 | opname = "max"; break; | |
1983 | case MIN_EXPR: | |
1984 | opname = "min"; break; | |
1985 | case EQ_EXPR: | |
1986 | opname = "=="; break; | |
1987 | case NE_EXPR: | |
1988 | opname = "!="; break; | |
1989 | case LE_EXPR: | |
1990 | opname = "<="; break; | |
1991 | case GE_EXPR: | |
1992 | opname = ">="; break; | |
1993 | case LT_EXPR: | |
1994 | opname = "<"; break; | |
1995 | case GT_EXPR: | |
1996 | opname = ">"; break; | |
1997 | case LSHIFT_EXPR: | |
1998 | opname = "<<"; break; | |
1999 | case RSHIFT_EXPR: | |
2000 | opname = ">>"; break; | |
2001 | case TRUNC_MOD_EXPR: | |
047de90b | 2002 | case FLOOR_MOD_EXPR: |
b30f223b RS |
2003 | opname = "%"; break; |
2004 | case TRUNC_DIV_EXPR: | |
047de90b | 2005 | case FLOOR_DIV_EXPR: |
b30f223b RS |
2006 | opname = "/"; break; |
2007 | case BIT_AND_EXPR: | |
2008 | opname = "&"; break; | |
2009 | case BIT_IOR_EXPR: | |
2010 | opname = "|"; break; | |
2011 | case TRUTH_ANDIF_EXPR: | |
2012 | opname = "&&"; break; | |
2013 | case TRUTH_ORIF_EXPR: | |
2014 | opname = "||"; break; | |
2015 | case BIT_XOR_EXPR: | |
2016 | opname = "^"; break; | |
047de90b RS |
2017 | case LROTATE_EXPR: |
2018 | case RROTATE_EXPR: | |
2019 | opname = "rotate"; break; | |
6d819282 MK |
2020 | default: |
2021 | opname = "unknown"; break; | |
b30f223b RS |
2022 | } |
2023 | error ("invalid operands to binary %s", opname); | |
2024 | } | |
2025 | \f | |
2026 | /* Subroutine of build_binary_op, used for comparison operations. | |
2027 | See if the operands have both been converted from subword integer types | |
2028 | and, if so, perhaps change them both back to their original type. | |
94dccd9d RS |
2029 | This function is also responsible for converting the two operands |
2030 | to the proper common type for comparison. | |
b30f223b RS |
2031 | |
2032 | The arguments of this function are all pointers to local variables | |
2033 | of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, | |
2034 | RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. | |
2035 | ||
2036 | If this function returns nonzero, it means that the comparison has | |
2037 | a constant value. What this function returns is an expression for | |
2038 | that value. */ | |
2039 | ||
2040 | tree | |
2041 | shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr) | |
2042 | tree *op0_ptr, *op1_ptr; | |
2043 | tree *restype_ptr; | |
2044 | enum tree_code *rescode_ptr; | |
2045 | { | |
2046 | register tree type; | |
2047 | tree op0 = *op0_ptr; | |
2048 | tree op1 = *op1_ptr; | |
2049 | int unsignedp0, unsignedp1; | |
2050 | int real1, real2; | |
2051 | tree primop0, primop1; | |
2052 | enum tree_code code = *rescode_ptr; | |
2053 | ||
2054 | /* Throw away any conversions to wider types | |
2055 | already present in the operands. */ | |
2056 | ||
2057 | primop0 = get_narrower (op0, &unsignedp0); | |
2058 | primop1 = get_narrower (op1, &unsignedp1); | |
2059 | ||
2060 | /* Handle the case that OP0 does not *contain* a conversion | |
2061 | but it *requires* conversion to FINAL_TYPE. */ | |
2062 | ||
2063 | if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) | |
2064 | unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0)); | |
2065 | if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) | |
2066 | unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1)); | |
2067 | ||
2068 | /* If one of the operands must be floated, we cannot optimize. */ | |
2069 | real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; | |
2070 | real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; | |
2071 | ||
2072 | /* If first arg is constant, swap the args (changing operation | |
5af6001b RK |
2073 | so value is preserved), for canonicalization. Don't do this if |
2074 | the second arg is 0. */ | |
b30f223b | 2075 | |
5af6001b RK |
2076 | if (TREE_CONSTANT (primop0) |
2077 | && ! integer_zerop (primop1) && ! real_zerop (primop1)) | |
b30f223b RS |
2078 | { |
2079 | register tree tem = primop0; | |
2080 | register int temi = unsignedp0; | |
2081 | primop0 = primop1; | |
2082 | primop1 = tem; | |
2083 | tem = op0; | |
2084 | op0 = op1; | |
2085 | op1 = tem; | |
2086 | *op0_ptr = op0; | |
2087 | *op1_ptr = op1; | |
2088 | unsignedp0 = unsignedp1; | |
2089 | unsignedp1 = temi; | |
2090 | temi = real1; | |
2091 | real1 = real2; | |
2092 | real2 = temi; | |
2093 | ||
2094 | switch (code) | |
2095 | { | |
2096 | case LT_EXPR: | |
2097 | code = GT_EXPR; | |
2098 | break; | |
2099 | case GT_EXPR: | |
2100 | code = LT_EXPR; | |
2101 | break; | |
2102 | case LE_EXPR: | |
2103 | code = GE_EXPR; | |
2104 | break; | |
2105 | case GE_EXPR: | |
2106 | code = LE_EXPR; | |
2107 | break; | |
6d819282 MK |
2108 | default: |
2109 | break; | |
b30f223b RS |
2110 | } |
2111 | *rescode_ptr = code; | |
2112 | } | |
2113 | ||
2114 | /* If comparing an integer against a constant more bits wide, | |
2115 | maybe we can deduce a value of 1 or 0 independent of the data. | |
2116 | Or else truncate the constant now | |
2117 | rather than extend the variable at run time. | |
2118 | ||
2119 | This is only interesting if the constant is the wider arg. | |
2120 | Also, it is not safe if the constant is unsigned and the | |
2121 | variable arg is signed, since in this case the variable | |
2122 | would be sign-extended and then regarded as unsigned. | |
2123 | Our technique fails in this case because the lowest/highest | |
2124 | possible unsigned results don't follow naturally from the | |
2125 | lowest/highest possible values of the variable operand. | |
2126 | For just EQ_EXPR and NE_EXPR there is another technique that | |
2127 | could be used: see if the constant can be faithfully represented | |
2128 | in the other operand's type, by truncating it and reextending it | |
2129 | and see if that preserves the constant's value. */ | |
2130 | ||
2131 | if (!real1 && !real2 | |
2132 | && TREE_CODE (primop1) == INTEGER_CST | |
2133 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) | |
2134 | { | |
2135 | int min_gt, max_gt, min_lt, max_lt; | |
2136 | tree maxval, minval; | |
2137 | /* 1 if comparison is nominally unsigned. */ | |
2138 | int unsignedp = TREE_UNSIGNED (*restype_ptr); | |
2139 | tree val; | |
2140 | ||
2141 | type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)); | |
8bbd5685 CW |
2142 | |
2143 | /* If TYPE is an enumeration, then we need to get its min/max | |
2144 | values from it's underlying integral type, not the enumerated | |
2145 | type itself. */ | |
2146 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
2147 | type = type_for_size (TYPE_PRECISION (type), unsignedp0); | |
b30f223b RS |
2148 | |
2149 | maxval = TYPE_MAX_VALUE (type); | |
2150 | minval = TYPE_MIN_VALUE (type); | |
2151 | ||
2152 | if (unsignedp && !unsignedp0) | |
2153 | *restype_ptr = signed_type (*restype_ptr); | |
2154 | ||
2155 | if (TREE_TYPE (primop1) != *restype_ptr) | |
2156 | primop1 = convert (*restype_ptr, primop1); | |
2157 | if (type != *restype_ptr) | |
2158 | { | |
2159 | minval = convert (*restype_ptr, minval); | |
2160 | maxval = convert (*restype_ptr, maxval); | |
2161 | } | |
2162 | ||
2163 | if (unsignedp && unsignedp0) | |
2164 | { | |
2165 | min_gt = INT_CST_LT_UNSIGNED (primop1, minval); | |
2166 | max_gt = INT_CST_LT_UNSIGNED (primop1, maxval); | |
2167 | min_lt = INT_CST_LT_UNSIGNED (minval, primop1); | |
2168 | max_lt = INT_CST_LT_UNSIGNED (maxval, primop1); | |
2169 | } | |
2170 | else | |
2171 | { | |
2172 | min_gt = INT_CST_LT (primop1, minval); | |
2173 | max_gt = INT_CST_LT (primop1, maxval); | |
2174 | min_lt = INT_CST_LT (minval, primop1); | |
2175 | max_lt = INT_CST_LT (maxval, primop1); | |
2176 | } | |
2177 | ||
2178 | val = 0; | |
2179 | /* This used to be a switch, but Genix compiler can't handle that. */ | |
2180 | if (code == NE_EXPR) | |
2181 | { | |
2182 | if (max_lt || min_gt) | |
a360da3a | 2183 | val = boolean_true_node; |
b30f223b RS |
2184 | } |
2185 | else if (code == EQ_EXPR) | |
2186 | { | |
2187 | if (max_lt || min_gt) | |
a360da3a | 2188 | val = boolean_false_node; |
b30f223b RS |
2189 | } |
2190 | else if (code == LT_EXPR) | |
2191 | { | |
2192 | if (max_lt) | |
a360da3a | 2193 | val = boolean_true_node; |
b30f223b | 2194 | if (!min_lt) |
a360da3a | 2195 | val = boolean_false_node; |
b30f223b RS |
2196 | } |
2197 | else if (code == GT_EXPR) | |
2198 | { | |
2199 | if (min_gt) | |
a360da3a | 2200 | val = boolean_true_node; |
b30f223b | 2201 | if (!max_gt) |
a360da3a | 2202 | val = boolean_false_node; |
b30f223b RS |
2203 | } |
2204 | else if (code == LE_EXPR) | |
2205 | { | |
2206 | if (!max_gt) | |
a360da3a | 2207 | val = boolean_true_node; |
b30f223b | 2208 | if (min_gt) |
a360da3a | 2209 | val = boolean_false_node; |
b30f223b RS |
2210 | } |
2211 | else if (code == GE_EXPR) | |
2212 | { | |
2213 | if (!min_lt) | |
a360da3a | 2214 | val = boolean_true_node; |
b30f223b | 2215 | if (max_lt) |
a360da3a | 2216 | val = boolean_false_node; |
b30f223b RS |
2217 | } |
2218 | ||
2219 | /* If primop0 was sign-extended and unsigned comparison specd, | |
2220 | we did a signed comparison above using the signed type bounds. | |
2221 | But the comparison we output must be unsigned. | |
2222 | ||
2223 | Also, for inequalities, VAL is no good; but if the signed | |
2224 | comparison had *any* fixed result, it follows that the | |
2225 | unsigned comparison just tests the sign in reverse | |
2226 | (positive values are LE, negative ones GE). | |
2227 | So we can generate an unsigned comparison | |
2228 | against an extreme value of the signed type. */ | |
2229 | ||
2230 | if (unsignedp && !unsignedp0) | |
2231 | { | |
2232 | if (val != 0) | |
2233 | switch (code) | |
2234 | { | |
2235 | case LT_EXPR: | |
2236 | case GE_EXPR: | |
2237 | primop1 = TYPE_MIN_VALUE (type); | |
2238 | val = 0; | |
2239 | break; | |
2240 | ||
2241 | case LE_EXPR: | |
2242 | case GT_EXPR: | |
2243 | primop1 = TYPE_MAX_VALUE (type); | |
2244 | val = 0; | |
2245 | break; | |
6d819282 MK |
2246 | |
2247 | default: | |
2248 | break; | |
b30f223b RS |
2249 | } |
2250 | type = unsigned_type (type); | |
2251 | } | |
2252 | ||
b7c9c707 | 2253 | if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST) |
b30f223b RS |
2254 | { |
2255 | /* This is the case of (char)x >?< 0x80, which people used to use | |
2256 | expecting old C compilers to change the 0x80 into -0x80. */ | |
a360da3a | 2257 | if (val == boolean_false_node) |
07be2a23 | 2258 | warning ("comparison is always false due to limited range of data type"); |
a360da3a | 2259 | if (val == boolean_true_node) |
07be2a23 | 2260 | warning ("comparison is always true due to limited range of data type"); |
b30f223b RS |
2261 | } |
2262 | ||
b7c9c707 | 2263 | if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST) |
b30f223b | 2264 | { |
1e276c4a | 2265 | /* This is the case of (unsigned char)x >?< -1 or < 0. */ |
a360da3a | 2266 | if (val == boolean_false_node) |
07be2a23 | 2267 | warning ("comparison is always false due to limited range of data type"); |
a360da3a | 2268 | if (val == boolean_true_node) |
07be2a23 | 2269 | warning ("comparison is always true due to limited range of data type"); |
b30f223b RS |
2270 | } |
2271 | ||
2272 | if (val != 0) | |
2273 | { | |
2274 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
2275 | if (TREE_SIDE_EFFECTS (primop0)) | |
2276 | return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); | |
2277 | return val; | |
2278 | } | |
2279 | ||
2280 | /* Value is not predetermined, but do the comparison | |
2281 | in the type of the operand that is not constant. | |
2282 | TYPE is already properly set. */ | |
2283 | } | |
2284 | else if (real1 && real2 | |
766f6c30 RS |
2285 | && (TYPE_PRECISION (TREE_TYPE (primop0)) |
2286 | == TYPE_PRECISION (TREE_TYPE (primop1)))) | |
b30f223b RS |
2287 | type = TREE_TYPE (primop0); |
2288 | ||
2289 | /* If args' natural types are both narrower than nominal type | |
2290 | and both extend in the same manner, compare them | |
2291 | in the type of the wider arg. | |
2292 | Otherwise must actually extend both to the nominal | |
2293 | common type lest different ways of extending | |
2294 | alter the result. | |
2295 | (eg, (short)-1 == (unsigned short)-1 should be 0.) */ | |
2296 | ||
2297 | else if (unsignedp0 == unsignedp1 && real1 == real2 | |
2298 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) | |
2299 | && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) | |
2300 | { | |
2301 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
2302 | type = signed_or_unsigned_type (unsignedp0 | |
2303 | || TREE_UNSIGNED (*restype_ptr), | |
2304 | type); | |
2305 | /* Make sure shorter operand is extended the right way | |
2306 | to match the longer operand. */ | |
2307 | primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)), | |
2308 | primop0); | |
2309 | primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)), | |
2310 | primop1); | |
2311 | } | |
2312 | else | |
2313 | { | |
2314 | /* Here we must do the comparison on the nominal type | |
2315 | using the args exactly as we received them. */ | |
2316 | type = *restype_ptr; | |
2317 | primop0 = op0; | |
2318 | primop1 = op1; | |
2319 | ||
2320 | if (!real1 && !real2 && integer_zerop (primop1) | |
597681f6 | 2321 | && TREE_UNSIGNED (*restype_ptr)) |
b30f223b RS |
2322 | { |
2323 | tree value = 0; | |
2324 | switch (code) | |
2325 | { | |
2326 | case GE_EXPR: | |
5af6001b RK |
2327 | /* All unsigned values are >= 0, so we warn if extra warnings |
2328 | are requested. However, if OP0 is a constant that is | |
2329 | >= 0, the signedness of the comparison isn't an issue, | |
2330 | so suppress the warning. */ | |
2c492eef | 2331 | if (extra_warnings && !in_system_header |
5af6001b RK |
2332 | && ! (TREE_CODE (primop0) == INTEGER_CST |
2333 | && ! TREE_OVERFLOW (convert (signed_type (type), | |
2334 | primop0)))) | |
07be2a23 | 2335 | warning ("comparison of unsigned expression >= 0 is always true"); |
a360da3a | 2336 | value = boolean_true_node; |
b30f223b RS |
2337 | break; |
2338 | ||
2339 | case LT_EXPR: | |
2c492eef | 2340 | if (extra_warnings && !in_system_header |
5af6001b RK |
2341 | && ! (TREE_CODE (primop0) == INTEGER_CST |
2342 | && ! TREE_OVERFLOW (convert (signed_type (type), | |
2343 | primop0)))) | |
07be2a23 | 2344 | warning ("comparison of unsigned expression < 0 is always false"); |
a360da3a | 2345 | value = boolean_false_node; |
6d819282 MK |
2346 | break; |
2347 | ||
2348 | default: | |
2349 | break; | |
b30f223b RS |
2350 | } |
2351 | ||
2352 | if (value != 0) | |
2353 | { | |
2354 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
2355 | if (TREE_SIDE_EFFECTS (primop0)) | |
2356 | return build (COMPOUND_EXPR, TREE_TYPE (value), | |
2357 | primop0, value); | |
2358 | return value; | |
2359 | } | |
2360 | } | |
2361 | } | |
2362 | ||
2363 | *op0_ptr = convert (type, primop0); | |
2364 | *op1_ptr = convert (type, primop1); | |
2365 | ||
a360da3a | 2366 | *restype_ptr = boolean_type_node; |
b30f223b RS |
2367 | |
2368 | return 0; | |
2369 | } | |
2370 | \f | |
2371 | /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, | |
2372 | or validate its data type for an `if' or `while' statement or ?..: exp. | |
2373 | ||
2374 | This preparation consists of taking the ordinary | |
2375 | representation of an expression expr and producing a valid tree | |
2376 | boolean expression describing whether expr is nonzero. We could | |
a360da3a | 2377 | simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1), |
b30f223b RS |
2378 | but we optimize comparisons, &&, ||, and !. |
2379 | ||
a360da3a | 2380 | The resulting type should always be `boolean_type_node'. */ |
b30f223b RS |
2381 | |
2382 | tree | |
2383 | truthvalue_conversion (expr) | |
2384 | tree expr; | |
2385 | { | |
257e61ed RS |
2386 | if (TREE_CODE (expr) == ERROR_MARK) |
2387 | return expr; | |
2388 | ||
d7c83727 | 2389 | #if 0 /* This appears to be wrong for C++. */ |
257e61ed RS |
2390 | /* These really should return error_mark_node after 2.4 is stable. |
2391 | But not all callers handle ERROR_MARK properly. */ | |
2392 | switch (TREE_CODE (TREE_TYPE (expr))) | |
2393 | { | |
2394 | case RECORD_TYPE: | |
2395 | error ("struct type value used where scalar is required"); | |
a360da3a | 2396 | return boolean_false_node; |
257e61ed RS |
2397 | |
2398 | case UNION_TYPE: | |
2399 | error ("union type value used where scalar is required"); | |
a360da3a | 2400 | return boolean_false_node; |
257e61ed RS |
2401 | |
2402 | case ARRAY_TYPE: | |
2403 | error ("array type value used where scalar is required"); | |
a360da3a | 2404 | return boolean_false_node; |
257e61ed RS |
2405 | |
2406 | default: | |
2407 | break; | |
2408 | } | |
d7c83727 | 2409 | #endif /* 0 */ |
257e61ed | 2410 | |
b30f223b RS |
2411 | switch (TREE_CODE (expr)) |
2412 | { | |
b30f223b | 2413 | case EQ_EXPR: |
b30f223b RS |
2414 | case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: |
2415 | case TRUTH_ANDIF_EXPR: | |
2416 | case TRUTH_ORIF_EXPR: | |
2417 | case TRUTH_AND_EXPR: | |
2418 | case TRUTH_OR_EXPR: | |
9379fac9 | 2419 | case TRUTH_XOR_EXPR: |
1180eb10 | 2420 | case TRUTH_NOT_EXPR: |
a360da3a JM |
2421 | TREE_TYPE (expr) = boolean_type_node; |
2422 | return expr; | |
18c0f675 | 2423 | |
b30f223b RS |
2424 | case ERROR_MARK: |
2425 | return expr; | |
2426 | ||
2427 | case INTEGER_CST: | |
a360da3a | 2428 | return integer_zerop (expr) ? boolean_false_node : boolean_true_node; |
b30f223b RS |
2429 | |
2430 | case REAL_CST: | |
a360da3a | 2431 | return real_zerop (expr) ? boolean_false_node : boolean_true_node; |
b30f223b RS |
2432 | |
2433 | case ADDR_EXPR: | |
fc0c675f RK |
2434 | /* If we are taking the address of a external decl, it might be zero |
2435 | if it is weak, so we cannot optimize. */ | |
2f939d94 | 2436 | if (DECL_P (TREE_OPERAND (expr, 0)) |
fc0c675f RK |
2437 | && DECL_EXTERNAL (TREE_OPERAND (expr, 0))) |
2438 | break; | |
2439 | ||
b30f223b | 2440 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0))) |
a360da3a JM |
2441 | return build (COMPOUND_EXPR, boolean_type_node, |
2442 | TREE_OPERAND (expr, 0), boolean_true_node); | |
b30f223b | 2443 | else |
a360da3a | 2444 | return boolean_true_node; |
b30f223b | 2445 | |
766f6c30 | 2446 | case COMPLEX_EXPR: |
f0b996c5 | 2447 | return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) |
b839fb3f | 2448 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
f0b996c5 RS |
2449 | truthvalue_conversion (TREE_OPERAND (expr, 0)), |
2450 | truthvalue_conversion (TREE_OPERAND (expr, 1)), | |
766f6c30 RS |
2451 | 0); |
2452 | ||
b30f223b RS |
2453 | case NEGATE_EXPR: |
2454 | case ABS_EXPR: | |
2455 | case FLOAT_EXPR: | |
2456 | case FFS_EXPR: | |
2457 | /* These don't change whether an object is non-zero or zero. */ | |
2458 | return truthvalue_conversion (TREE_OPERAND (expr, 0)); | |
2459 | ||
2460 | case LROTATE_EXPR: | |
2461 | case RROTATE_EXPR: | |
2462 | /* These don't change whether an object is zero or non-zero, but | |
2463 | we can't ignore them if their second arg has side-effects. */ | |
2464 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) | |
a360da3a | 2465 | return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1), |
b30f223b RS |
2466 | truthvalue_conversion (TREE_OPERAND (expr, 0))); |
2467 | else | |
2468 | return truthvalue_conversion (TREE_OPERAND (expr, 0)); | |
b57062ca | 2469 | |
b30f223b RS |
2470 | case COND_EXPR: |
2471 | /* Distribute the conversion into the arms of a COND_EXPR. */ | |
a360da3a | 2472 | return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0), |
b30f223b RS |
2473 | truthvalue_conversion (TREE_OPERAND (expr, 1)), |
2474 | truthvalue_conversion (TREE_OPERAND (expr, 2)))); | |
2475 | ||
2476 | case CONVERT_EXPR: | |
2477 | /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, | |
2478 | since that affects how `default_conversion' will behave. */ | |
2479 | if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE | |
2480 | || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE) | |
2481 | break; | |
0f41302f | 2482 | /* fall through... */ |
b30f223b RS |
2483 | case NOP_EXPR: |
2484 | /* If this is widening the argument, we can ignore it. */ | |
2485 | if (TYPE_PRECISION (TREE_TYPE (expr)) | |
2486 | >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0)))) | |
2487 | return truthvalue_conversion (TREE_OPERAND (expr, 0)); | |
2488 | break; | |
2489 | ||
b30f223b | 2490 | case MINUS_EXPR: |
f87550e0 JW |
2491 | /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize |
2492 | this case. */ | |
2493 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT | |
2494 | && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE) | |
2495 | break; | |
0f41302f | 2496 | /* fall through... */ |
f87550e0 | 2497 | case BIT_XOR_EXPR: |
d7c83727 | 2498 | /* This and MINUS_EXPR can be changed into a comparison of the |
f87550e0 | 2499 | two objects. */ |
b30f223b RS |
2500 | if (TREE_TYPE (TREE_OPERAND (expr, 0)) |
2501 | == TREE_TYPE (TREE_OPERAND (expr, 1))) | |
2502 | return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0), | |
2503 | TREE_OPERAND (expr, 1), 1); | |
2504 | return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0), | |
2505 | fold (build1 (NOP_EXPR, | |
2506 | TREE_TYPE (TREE_OPERAND (expr, 0)), | |
2507 | TREE_OPERAND (expr, 1))), 1); | |
e2aab13d | 2508 | |
fb48b1f0 | 2509 | case BIT_AND_EXPR: |
58cee643 RK |
2510 | if (integer_onep (TREE_OPERAND (expr, 1)) |
2511 | && TREE_TYPE (expr) != boolean_type_node) | |
2512 | /* Using convert here would cause infinite recursion. */ | |
2513 | return build1 (NOP_EXPR, boolean_type_node, expr); | |
2514 | break; | |
fb48b1f0 | 2515 | |
e2aab13d RS |
2516 | case MODIFY_EXPR: |
2517 | if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR) | |
2518 | warning ("suggest parentheses around assignment used as truth value"); | |
2519 | break; | |
b57062ca | 2520 | |
6d819282 MK |
2521 | default: |
2522 | break; | |
b30f223b RS |
2523 | } |
2524 | ||
f0b996c5 | 2525 | if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) |
f0b8d9aa AS |
2526 | { |
2527 | tree tem = save_expr (expr); | |
2528 | return (build_binary_op | |
2529 | ((TREE_SIDE_EFFECTS (expr) | |
2530 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), | |
2531 | truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)), | |
2532 | truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)), | |
2533 | 0)); | |
2534 | } | |
f0b996c5 | 2535 | |
b30f223b RS |
2536 | return build_binary_op (NE_EXPR, expr, integer_zero_node, 1); |
2537 | } | |
2538 | \f | |
fc2aaf30 JM |
2539 | static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree, |
2540 | int, enum built_in_class, int, int, | |
2541 | int)); | |
2542 | ||
0b73773c NH |
2543 | /* Make a variant type in the proper way for C/C++, propagating qualifiers |
2544 | down to the element type of an array. */ | |
2545 | ||
2546 | tree | |
3932261a | 2547 | c_build_qualified_type (type, type_quals) |
0b73773c | 2548 | tree type; |
3932261a | 2549 | int type_quals; |
0b73773c | 2550 | { |
3932261a MM |
2551 | /* A restrict-qualified pointer type must be a pointer to object or |
2552 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
2553 | REFERENCE_TYPEs, which is appropriate for C++. Unfortunately, | |
2554 | the C++ front-end also use POINTER_TYPE for pointer-to-member | |
2555 | values, so even though it should be illegal to use `restrict' | |
2556 | with such an entity we don't flag that here. Thus, special case | |
2557 | code for that case is required in the C++ front-end. */ | |
2558 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
2559 | && (!POINTER_TYPE_P (type) | |
2560 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
2561 | { | |
2562 | error ("invalid use of `restrict'"); | |
2563 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
2564 | } | |
2565 | ||
0b73773c | 2566 | if (TREE_CODE (type) == ARRAY_TYPE) |
3932261a MM |
2567 | return build_array_type (c_build_qualified_type (TREE_TYPE (type), |
2568 | type_quals), | |
3ab1999b | 2569 | TYPE_DOMAIN (type)); |
3932261a MM |
2570 | return build_qualified_type (type, type_quals); |
2571 | } | |
2572 | ||
2573 | /* Apply the TYPE_QUALS to the new DECL. */ | |
2574 | ||
2575 | void | |
2576 | c_apply_type_quals_to_decl (type_quals, decl) | |
2577 | int type_quals; | |
2578 | tree decl; | |
2579 | { | |
a6496605 MM |
2580 | if ((type_quals & TYPE_QUAL_CONST) |
2581 | || (TREE_TYPE (decl) | |
2582 | && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)) | |
3932261a MM |
2583 | TREE_READONLY (decl) = 1; |
2584 | if (type_quals & TYPE_QUAL_VOLATILE) | |
2585 | { | |
2586 | TREE_SIDE_EFFECTS (decl) = 1; | |
2587 | TREE_THIS_VOLATILE (decl) = 1; | |
2588 | } | |
6946bc60 | 2589 | if (type_quals & TYPE_QUAL_RESTRICT) |
3932261a | 2590 | { |
6946bc60 MM |
2591 | if (!TREE_TYPE (decl) |
2592 | || !POINTER_TYPE_P (TREE_TYPE (decl)) | |
2593 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl)))) | |
2594 | error ("invalid use of `restrict'"); | |
2595 | else if (flag_strict_aliasing) | |
3932261a | 2596 | { |
6946bc60 MM |
2597 | /* No two restricted pointers can point at the same thing. |
2598 | However, a restricted pointer can point at the same thing | |
2599 | as an unrestricted pointer, if that unrestricted pointer | |
2600 | is based on the restricted pointer. So, we make the | |
2601 | alias set for the restricted pointer a subset of the | |
2602 | alias set for the type pointed to by the type of the | |
2603 | decl. */ | |
2604 | ||
3bdf5ad1 | 2605 | HOST_WIDE_INT pointed_to_alias_set |
6946bc60 | 2606 | = get_alias_set (TREE_TYPE (TREE_TYPE (decl))); |
d125d268 | 2607 | |
3bdf5ad1 | 2608 | if (pointed_to_alias_set == 0) |
6946bc60 MM |
2609 | /* It's not legal to make a subset of alias set zero. */ |
2610 | ; | |
2611 | else | |
2612 | { | |
2613 | DECL_POINTER_ALIAS_SET (decl) = new_alias_set (); | |
2614 | record_alias_subset (pointed_to_alias_set, | |
2615 | DECL_POINTER_ALIAS_SET (decl)); | |
2616 | } | |
3932261a MM |
2617 | } |
2618 | } | |
2619 | } | |
2620 | ||
41472af8 MM |
2621 | |
2622 | /* Return the typed-based alias set for T, which may be an expression | |
3bdf5ad1 | 2623 | or a type. Return -1 if we don't do anything special. */ |
41472af8 | 2624 | |
3bdf5ad1 | 2625 | HOST_WIDE_INT |
2e761e49 | 2626 | lang_get_alias_set (t) |
41472af8 MM |
2627 | tree t; |
2628 | { | |
08bc2431 | 2629 | tree u; |
604bb87d DB |
2630 | |
2631 | /* We know nothing about vector types */ | |
2632 | if (TREE_CODE (t) == VECTOR_TYPE) | |
2633 | return 0; | |
2634 | ||
08bc2431 MM |
2635 | /* Permit type-punning when accessing a union, provided the access |
2636 | is directly through the union. For example, this code does not | |
2637 | permit taking the address of a union member and then storing | |
2638 | through it. Even the type-punning allowed here is a GCC | |
2639 | extension, albeit a common and useful one; the C standard says | |
2640 | that such accesses have implementation-defined behavior. */ | |
2641 | for (u = t; | |
2642 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
2643 | u = TREE_OPERAND (u, 0)) | |
2644 | if (TREE_CODE (u) == COMPONENT_REF | |
2645 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
2646 | return 0; | |
ece32014 | 2647 | |
3bdf5ad1 | 2648 | /* If this is a char *, the ANSI C standard says it can alias |
f824e5c3 RK |
2649 | anything. Note that all references need do this. */ |
2650 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r' | |
2651 | && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE | |
2652 | && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node)) | |
3bdf5ad1 | 2653 | return 0; |
3932261a | 2654 | |
3bdf5ad1 RK |
2655 | /* That's all the expressions we handle specially. */ |
2656 | if (! TYPE_P (t)) | |
2657 | return -1; | |
41472af8 | 2658 | |
f824e5c3 RK |
2659 | /* The C standard specifically allows aliasing between signed and |
2660 | unsigned variants of the same type. We treat the signed | |
2661 | variant as canonical. */ | |
2662 | if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t)) | |
8f215dce JJ |
2663 | { |
2664 | tree t1 = signed_type (t); | |
f824e5c3 | 2665 | |
8f215dce JJ |
2666 | /* t1 == t can happen for boolean nodes which are always unsigned. */ |
2667 | if (t1 != t) | |
2668 | return get_alias_set (t1); | |
2669 | } | |
3bdf5ad1 | 2670 | else if (POINTER_TYPE_P (t)) |
02af3af6 | 2671 | { |
3bdf5ad1 | 2672 | tree t1; |
02af3af6 MS |
2673 | |
2674 | /* Unfortunately, there is no canonical form of a pointer type. | |
2675 | In particular, if we have `typedef int I', then `int *', and | |
2676 | `I *' are different types. So, we have to pick a canonical | |
2677 | representative. We do this below. | |
d125d268 | 2678 | |
b61148dd MM |
2679 | Technically, this approach is actually more conservative that |
2680 | it needs to be. In particular, `const int *' and `int *' | |
2681 | chould be in different alias sets, according to the C and C++ | |
2682 | standard, since their types are not the same, and so, | |
2683 | technically, an `int **' and `const int **' cannot point at | |
2684 | the same thing. | |
2685 | ||
2686 | But, the standard is wrong. In particular, this code is | |
2687 | legal C++: | |
2688 | ||
2689 | int *ip; | |
2690 | int **ipp = &ip; | |
2691 | const int* const* cipp = &ip; | |
2692 | ||
2693 | And, it doesn't make sense for that to be legal unless you | |
2694 | can dereference IPP and CIPP. So, we ignore cv-qualifiers on | |
2695 | the pointed-to types. This issue has been reported to the | |
2696 | C++ committee. */ | |
12e1243e | 2697 | t1 = build_type_no_quals (t); |
3bdf5ad1 RK |
2698 | if (t1 != t) |
2699 | return get_alias_set (t1); | |
02af3af6 | 2700 | } |
5b2abab8 MM |
2701 | /* It's not yet safe to use alias sets for classes in C++ because |
2702 | the TYPE_FIELDs list for a class doesn't mention base classes. */ | |
f6f8291a | 2703 | else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t)) |
5b2abab8 | 2704 | return 0; |
ece32014 | 2705 | |
3bdf5ad1 | 2706 | return -1; |
41472af8 | 2707 | } |
7f4edbcb BS |
2708 | |
2709 | /* Build tree nodes and builtin functions common to both C and C++ language | |
6bcedb4e | 2710 | frontends. */ |
3bdf5ad1 | 2711 | |
7f4edbcb | 2712 | void |
6bcedb4e | 2713 | c_common_nodes_and_builtins () |
7f4edbcb | 2714 | { |
eaa7c03f JM |
2715 | int wchar_type_size; |
2716 | tree array_domain_type; | |
7f4edbcb BS |
2717 | tree temp; |
2718 | tree memcpy_ftype, memset_ftype, strlen_ftype; | |
ad3fd36f | 2719 | tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype; |
18f988a0 | 2720 | tree fputs_ftype, fputc_ftype, fwrite_ftype, fprintf_ftype; |
7f4edbcb | 2721 | tree endlink, int_endlink, double_endlink, unsigned_endlink; |
60291474 | 2722 | tree cstring_endlink, sizetype_endlink; |
7f4edbcb | 2723 | tree ptr_ftype, ptr_ftype_unsigned; |
c2e42aab | 2724 | tree void_ftype_any, void_ftype_int, int_ftype_any; |
7f4edbcb BS |
2725 | tree double_ftype_double, double_ftype_double_double; |
2726 | tree float_ftype_float, ldouble_ftype_ldouble; | |
341e3d11 JM |
2727 | tree cfloat_ftype_cfloat, cdouble_ftype_cdouble, cldouble_ftype_cldouble; |
2728 | tree float_ftype_cfloat, double_ftype_cdouble, ldouble_ftype_cldouble; | |
d118937d | 2729 | tree int_ftype_cptr_cptr_sizet, sizet_ftype_cstring_cstring; |
60291474 KG |
2730 | tree int_ftype_cstring_cstring, string_ftype_string_cstring; |
2731 | tree string_ftype_cstring_int, string_ftype_cstring_cstring; | |
da9e9f08 | 2732 | tree string_ftype_string_cstring_sizet, int_ftype_cstring_cstring_sizet; |
7f4edbcb | 2733 | tree long_ftype_long; |
1c44c45d | 2734 | tree longlong_ftype_longlong; |
e78f4a97 | 2735 | tree intmax_ftype_intmax; |
7f4edbcb BS |
2736 | /* Either char* or void*. */ |
2737 | tree traditional_ptr_type_node; | |
4b2a62db KG |
2738 | /* Either const char* or const void*. */ |
2739 | tree traditional_cptr_type_node; | |
2740 | tree traditional_len_type_node; | |
2741 | tree traditional_len_endlink; | |
9f720c3e | 2742 | tree va_list_ref_type_node; |
daf68dd7 | 2743 | tree va_list_arg_type_node; |
d3707adb | 2744 | |
eaa7c03f JM |
2745 | /* Define `int' and `char' first so that dbx will output them first. */ |
2746 | record_builtin_type (RID_INT, NULL_PTR, integer_type_node); | |
2747 | record_builtin_type (RID_CHAR, "char", char_type_node); | |
2748 | ||
2749 | /* `signed' is the same as `int'. FIXME: the declarations of "signed", | |
2750 | "unsigned long", "long long unsigned" and "unsigned short" were in C++ | |
2751 | but not C. Are the conditionals here needed? */ | |
2752 | if (c_language == clk_cplusplus) | |
2753 | record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node); | |
2754 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); | |
2755 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); | |
2756 | record_builtin_type (RID_MAX, "long unsigned int", | |
2757 | long_unsigned_type_node); | |
2758 | if (c_language == clk_cplusplus) | |
2759 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); | |
2760 | record_builtin_type (RID_MAX, "long long int", | |
2761 | long_long_integer_type_node); | |
2762 | record_builtin_type (RID_MAX, "long long unsigned int", | |
2763 | long_long_unsigned_type_node); | |
2764 | if (c_language == clk_cplusplus) | |
2765 | record_builtin_type (RID_MAX, "long long unsigned", | |
2766 | long_long_unsigned_type_node); | |
2767 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); | |
2768 | record_builtin_type (RID_MAX, "short unsigned int", | |
2769 | short_unsigned_type_node); | |
2770 | if (c_language == clk_cplusplus) | |
2771 | record_builtin_type (RID_MAX, "unsigned short", | |
2772 | short_unsigned_type_node); | |
2773 | ||
2774 | /* Define both `signed char' and `unsigned char'. */ | |
2775 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); | |
2776 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); | |
2777 | ||
2778 | /* These are types that type_for_size and type_for_mode use. */ | |
2779 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node)); | |
2780 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node)); | |
2781 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node)); | |
2782 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node)); | |
2783 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
2784 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node)); | |
2785 | #endif | |
2786 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node)); | |
2787 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node)); | |
2788 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node)); | |
2789 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node)); | |
2790 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
2791 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node)); | |
2792 | #endif | |
2793 | ||
2794 | /* Create the widest literal types. */ | |
2795 | widest_integer_literal_type_node | |
2796 | = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
2797 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, | |
2798 | widest_integer_literal_type_node)); | |
2799 | ||
2800 | widest_unsigned_literal_type_node | |
2801 | = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
2802 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, | |
2803 | widest_unsigned_literal_type_node)); | |
2804 | ||
2805 | /* `unsigned long' is the standard type for sizeof. | |
2806 | Note that stddef.h uses `unsigned long', | |
2807 | and this must agree, even if long and int are the same size. */ | |
2808 | c_size_type_node = | |
2809 | TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE))); | |
2810 | signed_size_type_node = signed_type (c_size_type_node); | |
2811 | if (flag_traditional) | |
2812 | c_size_type_node = signed_size_type_node; | |
2813 | set_sizetype (c_size_type_node); | |
2814 | ||
2815 | build_common_tree_nodes_2 (flag_short_double); | |
2816 | ||
2817 | record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node); | |
2818 | record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node); | |
2819 | record_builtin_type (RID_MAX, "long double", long_double_type_node); | |
2820 | ||
2821 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"), | |
2822 | complex_integer_type_node)); | |
2823 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"), | |
2824 | complex_float_type_node)); | |
2825 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"), | |
2826 | complex_double_type_node)); | |
2827 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"), | |
2828 | complex_long_double_type_node)); | |
2829 | ||
2830 | record_builtin_type (RID_VOID, NULL_PTR, void_type_node); | |
2831 | ||
2832 | void_list_node = build_void_list_node (); | |
2833 | ||
2834 | /* Make a type to be the domain of a few array types | |
2835 | whose domains don't really matter. | |
2836 | 200 is small enough that it always fits in size_t | |
2837 | and large enough that it can hold most function names for the | |
2838 | initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
2839 | array_domain_type = build_index_type (size_int (200)); | |
2840 | ||
2841 | /* Make a type for arrays of characters. | |
2842 | With luck nothing will ever really depend on the length of this | |
2843 | array type. */ | |
2844 | char_array_type_node | |
2845 | = build_array_type (char_type_node, array_domain_type); | |
2846 | ||
2847 | /* Likewise for arrays of ints. */ | |
2848 | int_array_type_node | |
2849 | = build_array_type (integer_type_node, array_domain_type); | |
2850 | ||
2851 | #ifdef MD_INIT_BUILTINS | |
2852 | MD_INIT_BUILTINS; | |
2853 | #endif | |
2854 | ||
2855 | /* This is special for C++ so functions can be overloaded. */ | |
2856 | wchar_type_node = get_identifier (flag_short_wchar | |
2857 | ? "short unsigned int" | |
2858 | : WCHAR_TYPE); | |
2859 | wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); | |
2860 | wchar_type_size = TYPE_PRECISION (wchar_type_node); | |
2861 | if (c_language == clk_cplusplus) | |
2862 | { | |
2863 | if (TREE_UNSIGNED (wchar_type_node)) | |
2864 | wchar_type_node = make_unsigned_type (wchar_type_size); | |
2865 | else | |
2866 | wchar_type_node = make_signed_type (wchar_type_size); | |
2867 | record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); | |
2868 | } | |
2869 | else | |
2870 | { | |
2871 | signed_wchar_type_node = signed_type (wchar_type_node); | |
2872 | unsigned_wchar_type_node = unsigned_type (wchar_type_node); | |
2873 | } | |
2874 | ||
2875 | /* This is for wide string constants. */ | |
2876 | wchar_array_type_node | |
2877 | = build_array_type (wchar_type_node, array_domain_type); | |
2878 | ||
5fd8e536 JM |
2879 | string_type_node = build_pointer_type (char_type_node); |
2880 | const_string_type_node | |
2881 | = build_pointer_type (build_type_variant (char_type_node, 1, 0)); | |
2882 | ||
2883 | wint_type_node = | |
2884 | TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); | |
2885 | ||
2886 | intmax_type_node = | |
2887 | TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); | |
2888 | uintmax_type_node = | |
2889 | TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); | |
2890 | ||
2891 | default_function_type = build_function_type (integer_type_node, NULL_TREE); | |
2892 | ptrdiff_type_node | |
2893 | = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); | |
2894 | unsigned_ptrdiff_type_node = unsigned_type (ptrdiff_type_node); | |
2895 | ||
d3707adb RH |
2896 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"), |
2897 | va_list_type_node)); | |
daf68dd7 | 2898 | |
29ae8f10 GRK |
2899 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"), |
2900 | ptrdiff_type_node)); | |
2901 | ||
2902 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"), | |
2903 | sizetype)); | |
2904 | ||
daf68dd7 | 2905 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
9f720c3e GK |
2906 | { |
2907 | va_list_arg_type_node = va_list_ref_type_node = | |
2908 | build_pointer_type (TREE_TYPE (va_list_type_node)); | |
2909 | } | |
daf68dd7 | 2910 | else |
9f720c3e GK |
2911 | { |
2912 | va_list_arg_type_node = va_list_type_node; | |
2913 | va_list_ref_type_node = build_reference_type (va_list_type_node); | |
2914 | } | |
2915 | ||
7f4edbcb BS |
2916 | endlink = void_list_node; |
2917 | int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink); | |
2918 | double_endlink = tree_cons (NULL_TREE, double_type_node, endlink); | |
2919 | unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink); | |
60291474 | 2920 | cstring_endlink = tree_cons (NULL_TREE, const_string_type_node, endlink); |
7f4edbcb BS |
2921 | |
2922 | ptr_ftype = build_function_type (ptr_type_node, NULL_TREE); | |
2923 | ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink); | |
21318741 | 2924 | sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink); |
7f4edbcb BS |
2925 | /* We realloc here because sizetype could be int or unsigned. S'ok. */ |
2926 | ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink); | |
2927 | ||
4b2a62db | 2928 | int_ftype_any = build_function_type (integer_type_node, NULL_TREE); |
7f4edbcb BS |
2929 | void_ftype_any = build_function_type (void_type_node, NULL_TREE); |
2930 | void_ftype = build_function_type (void_type_node, endlink); | |
2931 | void_ftype_int = build_function_type (void_type_node, int_endlink); | |
2932 | void_ftype_ptr | |
2933 | = build_function_type (void_type_node, | |
2934 | tree_cons (NULL_TREE, ptr_type_node, endlink)); | |
2935 | ||
2936 | float_ftype_float | |
2937 | = build_function_type (float_type_node, | |
2938 | tree_cons (NULL_TREE, float_type_node, endlink)); | |
2939 | ||
2940 | double_ftype_double | |
2941 | = build_function_type (double_type_node, double_endlink); | |
2942 | ||
2943 | ldouble_ftype_ldouble | |
2944 | = build_function_type (long_double_type_node, | |
2945 | tree_cons (NULL_TREE, long_double_type_node, | |
2946 | endlink)); | |
2947 | ||
2948 | double_ftype_double_double | |
2949 | = build_function_type (double_type_node, | |
2950 | tree_cons (NULL_TREE, double_type_node, | |
2951 | double_endlink)); | |
2952 | ||
341e3d11 JM |
2953 | cfloat_ftype_cfloat |
2954 | = build_function_type (complex_float_type_node, | |
2955 | tree_cons (NULL_TREE, complex_float_type_node, | |
2956 | endlink)); | |
2957 | cdouble_ftype_cdouble | |
2958 | = build_function_type (complex_double_type_node, | |
2959 | tree_cons (NULL_TREE, complex_double_type_node, | |
2960 | endlink)); | |
2961 | cldouble_ftype_cldouble | |
2962 | = build_function_type (complex_long_double_type_node, | |
2963 | tree_cons (NULL_TREE, complex_long_double_type_node, | |
2964 | endlink)); | |
2965 | ||
2966 | float_ftype_cfloat | |
2967 | = build_function_type (float_type_node, | |
2968 | tree_cons (NULL_TREE, complex_float_type_node, | |
2969 | endlink)); | |
2970 | double_ftype_cdouble | |
2971 | = build_function_type (double_type_node, | |
2972 | tree_cons (NULL_TREE, complex_double_type_node, | |
2973 | endlink)); | |
2974 | ldouble_ftype_cldouble | |
2975 | = build_function_type (long_double_type_node, | |
2976 | tree_cons (NULL_TREE, complex_long_double_type_node, | |
2977 | endlink)); | |
2978 | ||
7f4edbcb BS |
2979 | int_ftype_int |
2980 | = build_function_type (integer_type_node, int_endlink); | |
2981 | ||
2982 | long_ftype_long | |
2983 | = build_function_type (long_integer_type_node, | |
2984 | tree_cons (NULL_TREE, long_integer_type_node, | |
2985 | endlink)); | |
2986 | ||
1c44c45d JM |
2987 | longlong_ftype_longlong |
2988 | = build_function_type (long_long_integer_type_node, | |
2989 | tree_cons (NULL_TREE, long_long_integer_type_node, | |
2990 | endlink)); | |
2991 | ||
e78f4a97 JM |
2992 | intmax_ftype_intmax |
2993 | = build_function_type (intmax_type_node, | |
2994 | tree_cons (NULL_TREE, intmax_type_node, | |
2995 | endlink)); | |
2996 | ||
7f4edbcb BS |
2997 | int_ftype_cptr_cptr_sizet |
2998 | = build_function_type (integer_type_node, | |
2999 | tree_cons (NULL_TREE, const_ptr_type_node, | |
60291474 KG |
3000 | tree_cons (NULL_TREE, |
3001 | const_ptr_type_node, | |
3002 | sizetype_endlink))); | |
7f4edbcb | 3003 | |
ae499cce MM |
3004 | void_zero_node = build_int_2 (0, 0); |
3005 | TREE_TYPE (void_zero_node) = void_type_node; | |
3006 | ||
d118937d KG |
3007 | /* Prototype for strcpy/strcat. */ |
3008 | string_ftype_string_cstring | |
7f4edbcb BS |
3009 | = build_function_type (string_type_node, |
3010 | tree_cons (NULL_TREE, string_type_node, | |
60291474 | 3011 | cstring_endlink)); |
7f4edbcb | 3012 | |
d118937d | 3013 | /* Prototype for strncpy/strncat. */ |
da9e9f08 KG |
3014 | string_ftype_string_cstring_sizet |
3015 | = build_function_type (string_type_node, | |
3016 | tree_cons (NULL_TREE, string_type_node, | |
3017 | tree_cons (NULL_TREE, | |
3018 | const_string_type_node, | |
3019 | sizetype_endlink))); | |
3020 | ||
6bcedb4e MM |
3021 | traditional_len_type_node = ((flag_traditional && |
3022 | c_language != clk_cplusplus) | |
4b2a62db KG |
3023 | ? integer_type_node : sizetype); |
3024 | traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node, | |
3025 | endlink); | |
3026 | ||
7f4edbcb | 3027 | /* Prototype for strcmp. */ |
60291474 | 3028 | int_ftype_cstring_cstring |
7f4edbcb BS |
3029 | = build_function_type (integer_type_node, |
3030 | tree_cons (NULL_TREE, const_string_type_node, | |
60291474 | 3031 | cstring_endlink)); |
7f4edbcb | 3032 | |
d118937d KG |
3033 | /* Prototype for strspn/strcspn. */ |
3034 | sizet_ftype_cstring_cstring | |
3035 | = build_function_type (c_size_type_node, | |
3036 | tree_cons (NULL_TREE, const_string_type_node, | |
60291474 | 3037 | cstring_endlink)); |
d118937d | 3038 | |
da9e9f08 KG |
3039 | /* Prototype for strncmp. */ |
3040 | int_ftype_cstring_cstring_sizet | |
3041 | = build_function_type (integer_type_node, | |
3042 | tree_cons (NULL_TREE, const_string_type_node, | |
3043 | tree_cons (NULL_TREE, | |
3044 | const_string_type_node, | |
3045 | sizetype_endlink))); | |
3046 | ||
26a87cab | 3047 | /* Prototype for strstr, strpbrk, etc. */ |
60291474 | 3048 | string_ftype_cstring_cstring |
78e7629e KG |
3049 | = build_function_type (string_type_node, |
3050 | tree_cons (NULL_TREE, const_string_type_node, | |
60291474 | 3051 | cstring_endlink)); |
78e7629e KG |
3052 | |
3053 | /* Prototype for strchr. */ | |
60291474 | 3054 | string_ftype_cstring_int |
78e7629e KG |
3055 | = build_function_type (string_type_node, |
3056 | tree_cons (NULL_TREE, const_string_type_node, | |
60291474 | 3057 | int_endlink)); |
78e7629e | 3058 | |
7f4edbcb BS |
3059 | /* Prototype for strlen. */ |
3060 | strlen_ftype | |
60291474 | 3061 | = build_function_type (traditional_len_type_node, cstring_endlink); |
7f4edbcb | 3062 | |
6bcedb4e MM |
3063 | traditional_ptr_type_node = ((flag_traditional && |
3064 | c_language != clk_cplusplus) | |
7f4edbcb | 3065 | ? string_type_node : ptr_type_node); |
6bcedb4e MM |
3066 | traditional_cptr_type_node = ((flag_traditional && |
3067 | c_language != clk_cplusplus) | |
4b2a62db | 3068 | ? const_string_type_node : const_ptr_type_node); |
7f4edbcb BS |
3069 | |
3070 | /* Prototype for memcpy. */ | |
3071 | memcpy_ftype | |
3072 | = build_function_type (traditional_ptr_type_node, | |
3073 | tree_cons (NULL_TREE, ptr_type_node, | |
3074 | tree_cons (NULL_TREE, const_ptr_type_node, | |
3075 | sizetype_endlink))); | |
3076 | ||
3077 | /* Prototype for memset. */ | |
3078 | memset_ftype | |
3079 | = build_function_type (traditional_ptr_type_node, | |
3080 | tree_cons (NULL_TREE, ptr_type_node, | |
3081 | tree_cons (NULL_TREE, integer_type_node, | |
60291474 | 3082 | sizetype_endlink))); |
7f4edbcb | 3083 | |
e3a709be KG |
3084 | /* Prototype for bzero. */ |
3085 | bzero_ftype | |
3086 | = build_function_type (void_type_node, | |
3087 | tree_cons (NULL_TREE, traditional_ptr_type_node, | |
3088 | traditional_len_endlink)); | |
3089 | ||
4b2a62db KG |
3090 | /* Prototype for bcmp. */ |
3091 | bcmp_ftype | |
3092 | = build_function_type (integer_type_node, | |
3093 | tree_cons (NULL_TREE, traditional_cptr_type_node, | |
3094 | tree_cons (NULL_TREE, | |
3095 | traditional_cptr_type_node, | |
3096 | traditional_len_endlink))); | |
3097 | ||
ad3fd36f KG |
3098 | /* Prototype for puts. */ |
3099 | puts_ftype | |
60291474 | 3100 | = build_function_type (integer_type_node, cstring_endlink); |
ad3fd36f KG |
3101 | |
3102 | /* Prototype for printf. */ | |
3103 | printf_ftype | |
3104 | = build_function_type (integer_type_node, | |
3105 | tree_cons (NULL_TREE, const_string_type_node, | |
3106 | NULL_TREE)); | |
3107 | ||
c2e42aab KG |
3108 | /* These stdio prototypes are declared using void* in place of |
3109 | FILE*. They are only used for __builtin_ style calls, regular | |
3110 | style builtin prototypes omit the arguments and merge those | |
3111 | provided by stdio.h. */ | |
3112 | /* Prototype for fwrite. */ | |
3113 | fwrite_ftype | |
3114 | = build_function_type (c_size_type_node, | |
3115 | tree_cons (NULL_TREE, const_ptr_type_node, | |
3116 | tree_cons (NULL_TREE, c_size_type_node, | |
3117 | tree_cons (NULL_TREE, c_size_type_node, | |
3118 | tree_cons (NULL_TREE, ptr_type_node, endlink))))); | |
3119 | ||
3120 | /* Prototype for fputc. */ | |
3121 | fputc_ftype | |
3122 | = build_function_type (integer_type_node, | |
3123 | tree_cons (NULL_TREE, integer_type_node, | |
3124 | tree_cons (NULL_TREE, ptr_type_node, endlink))); | |
3125 | ||
3126 | /* Prototype for fputs. */ | |
3127 | fputs_ftype | |
3128 | = build_function_type (integer_type_node, | |
3129 | tree_cons (NULL_TREE, const_string_type_node, | |
3130 | tree_cons (NULL_TREE, ptr_type_node, endlink))); | |
3131 | ||
18f988a0 KG |
3132 | /* Prototype for fprintf. */ |
3133 | fprintf_ftype | |
3134 | = build_function_type (integer_type_node, | |
3135 | tree_cons (NULL_TREE, ptr_type_node, | |
3136 | tree_cons (NULL_TREE, | |
3137 | const_string_type_node, | |
3138 | NULL_TREE))); | |
3139 | ||
7f4edbcb | 3140 | builtin_function ("__builtin_constant_p", default_function_type, |
26db82d8 | 3141 | BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
3142 | |
3143 | builtin_function ("__builtin_return_address", ptr_ftype_unsigned, | |
26db82d8 | 3144 | BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
3145 | |
3146 | builtin_function ("__builtin_frame_address", ptr_ftype_unsigned, | |
26db82d8 | 3147 | BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
3148 | |
3149 | builtin_function ("__builtin_alloca", ptr_ftype_sizetype, | |
26db82d8 | 3150 | BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca"); |
fc2aaf30 JM |
3151 | builtin_function_2 ("__builtin_ffs", "ffs", |
3152 | int_ftype_int, int_ftype_int, | |
3153 | BUILT_IN_FFS, BUILT_IN_NORMAL, 0, 1, 0); | |
3154 | /* Define alloca as builtin, unless SMALL_STACK. */ | |
512b62fb | 3155 | #ifndef SMALL_STACK |
fc2aaf30 JM |
3156 | builtin_function_2 (NULL_PTR, "alloca", NULL_TREE, ptr_ftype_sizetype, |
3157 | BUILT_IN_ALLOCA, BUILT_IN_NORMAL, 0, 1, 0); | |
512b62fb | 3158 | #endif |
796cdb65 | 3159 | /* Declare _exit and _Exit just to mark them as non-returning. */ |
fc2aaf30 JM |
3160 | builtin_function_2 (NULL_PTR, "_exit", NULL_TREE, void_ftype_int, |
3161 | 0, NOT_BUILT_IN, 0, 1, 1); | |
796cdb65 JM |
3162 | builtin_function_2 (NULL_PTR, "_Exit", NULL_TREE, void_ftype_int, |
3163 | 0, NOT_BUILT_IN, 0, !flag_isoc99, 1); | |
fc2aaf30 JM |
3164 | |
3165 | builtin_function_2 ("__builtin_index", "index", | |
3166 | string_ftype_cstring_int, string_ftype_cstring_int, | |
3167 | BUILT_IN_INDEX, BUILT_IN_NORMAL, 1, 1, 0); | |
3168 | builtin_function_2 ("__builtin_rindex", "rindex", | |
3169 | string_ftype_cstring_int, string_ftype_cstring_int, | |
3170 | BUILT_IN_RINDEX, BUILT_IN_NORMAL, 1, 1, 0); | |
3171 | ||
3172 | /* The system prototypes for these functions have many | |
3173 | variations, so don't specify parameters to avoid conflicts. | |
3174 | The expand_* functions check the argument types anyway. */ | |
3175 | builtin_function_2 ("__builtin_bzero", "bzero", | |
3176 | bzero_ftype, void_ftype_any, | |
3177 | BUILT_IN_BZERO, BUILT_IN_NORMAL, 1, 1, 0); | |
3178 | builtin_function_2 ("__builtin_bcmp", "bcmp", | |
3179 | bcmp_ftype, int_ftype_any, | |
3180 | BUILT_IN_BCMP, BUILT_IN_NORMAL, 1, 1, 0); | |
3181 | ||
3182 | builtin_function_2 ("__builtin_abs", "abs", | |
3183 | int_ftype_int, int_ftype_int, | |
3184 | BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0); | |
3185 | builtin_function_2 ("__builtin_fabsf", "fabsf", | |
3186 | float_ftype_float, float_ftype_float, | |
3187 | BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0); | |
3188 | builtin_function_2 ("__builtin_fabs", "fabs", | |
3189 | double_ftype_double, double_ftype_double, | |
3190 | BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0); | |
3191 | builtin_function_2 ("__builtin_fabsl", "fabsl", | |
3192 | ldouble_ftype_ldouble, ldouble_ftype_ldouble, | |
3193 | BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0); | |
3194 | builtin_function_2 ("__builtin_labs", "labs", | |
3195 | long_ftype_long, long_ftype_long, | |
29b5ca41 | 3196 | BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0); |
fc2aaf30 JM |
3197 | builtin_function_2 ("__builtin_llabs", "llabs", |
3198 | longlong_ftype_longlong, longlong_ftype_longlong, | |
29b5ca41 | 3199 | BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); |
fc2aaf30 JM |
3200 | builtin_function_2 ("__builtin_imaxabs", "imaxabs", |
3201 | intmax_ftype_intmax, intmax_ftype_intmax, | |
29b5ca41 | 3202 | BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); |
7f4edbcb | 3203 | |
7f4edbcb | 3204 | builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS, |
26db82d8 | 3205 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3206 | builtin_function ("__builtin_classify_type", default_function_type, |
26db82d8 | 3207 | BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3208 | builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG, |
26db82d8 | 3209 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3210 | builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO, |
26db82d8 | 3211 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
3212 | builtin_function ("__builtin_setjmp", |
3213 | build_function_type (integer_type_node, | |
3214 | tree_cons (NULL_TREE, ptr_type_node, | |
3215 | endlink)), | |
26db82d8 | 3216 | BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
3217 | builtin_function ("__builtin_longjmp", |
3218 | build_function_type (void_type_node, | |
3219 | tree_cons (NULL_TREE, ptr_type_node, | |
60291474 | 3220 | int_endlink)), |
26db82d8 BS |
3221 | BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR); |
3222 | builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP, | |
3223 | BUILT_IN_NORMAL, NULL_PTR); | |
7f4edbcb | 3224 | |
1eb8759b RH |
3225 | /* ISO C99 IEEE Unordered compares. */ |
3226 | builtin_function ("__builtin_isgreater", default_function_type, | |
3227 | BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR); | |
3228 | builtin_function ("__builtin_isgreaterequal", default_function_type, | |
3229 | BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR); | |
3230 | builtin_function ("__builtin_isless", default_function_type, | |
3231 | BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR); | |
3232 | builtin_function ("__builtin_islessequal", default_function_type, | |
3233 | BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR); | |
3234 | builtin_function ("__builtin_islessgreater", default_function_type, | |
3235 | BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR); | |
3236 | builtin_function ("__builtin_isunordered", default_function_type, | |
3237 | BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR); | |
3238 | ||
7f4edbcb BS |
3239 | /* Untyped call and return. */ |
3240 | builtin_function ("__builtin_apply_args", ptr_ftype, | |
26db82d8 | 3241 | BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
3242 | |
3243 | temp = tree_cons (NULL_TREE, | |
3244 | build_pointer_type (build_function_type (void_type_node, | |
3245 | NULL_TREE)), | |
60291474 | 3246 | tree_cons (NULL_TREE, ptr_type_node, sizetype_endlink)); |
7f4edbcb BS |
3247 | builtin_function ("__builtin_apply", |
3248 | build_function_type (ptr_type_node, temp), | |
26db82d8 | 3249 | BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3250 | builtin_function ("__builtin_return", void_ftype_ptr, |
26db82d8 | 3251 | BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3252 | |
d3707adb RH |
3253 | /* Support for varargs.h and stdarg.h. */ |
3254 | builtin_function ("__builtin_varargs_start", | |
3255 | build_function_type (void_type_node, | |
3256 | tree_cons (NULL_TREE, | |
9f720c3e | 3257 | va_list_ref_type_node, |
d3707adb | 3258 | endlink)), |
26db82d8 | 3259 | BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR); |
d3707adb RH |
3260 | |
3261 | builtin_function ("__builtin_stdarg_start", | |
3262 | build_function_type (void_type_node, | |
3263 | tree_cons (NULL_TREE, | |
9f720c3e | 3264 | va_list_ref_type_node, |
d3707adb | 3265 | NULL_TREE)), |
26db82d8 | 3266 | BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR); |
d3707adb RH |
3267 | |
3268 | builtin_function ("__builtin_va_end", | |
3269 | build_function_type (void_type_node, | |
3270 | tree_cons (NULL_TREE, | |
9f720c3e | 3271 | va_list_ref_type_node, |
d3707adb | 3272 | endlink)), |
26db82d8 | 3273 | BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR); |
d3707adb RH |
3274 | |
3275 | builtin_function ("__builtin_va_copy", | |
3276 | build_function_type (void_type_node, | |
3277 | tree_cons (NULL_TREE, | |
9f720c3e | 3278 | va_list_ref_type_node, |
d3707adb | 3279 | tree_cons (NULL_TREE, |
daf68dd7 | 3280 | va_list_arg_type_node, |
d3707adb | 3281 | endlink))), |
26db82d8 | 3282 | BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR); |
d3707adb | 3283 | |
994a57cd RH |
3284 | /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */ |
3285 | builtin_function ("__builtin_expect", | |
3286 | build_function_type (long_integer_type_node, | |
3287 | tree_cons (NULL_TREE, | |
3288 | long_integer_type_node, | |
3289 | tree_cons (NULL_TREE, | |
3290 | long_integer_type_node, | |
3291 | endlink))), | |
3292 | BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR); | |
3293 | ||
7f4edbcb | 3294 | /* Currently under experimentation. */ |
fc2aaf30 JM |
3295 | builtin_function_2 ("__builtin_memcpy", "memcpy", |
3296 | memcpy_ftype, memcpy_ftype, | |
3297 | BUILT_IN_MEMCPY, BUILT_IN_NORMAL, 1, 0, 0); | |
3298 | builtin_function_2 ("__builtin_memcmp", "memcmp", | |
3299 | int_ftype_cptr_cptr_sizet, int_ftype_cptr_cptr_sizet, | |
3300 | BUILT_IN_MEMCMP, BUILT_IN_NORMAL, 1, 0, 0); | |
3301 | builtin_function_2 ("__builtin_memset", "memset", | |
3302 | memset_ftype, memset_ftype, | |
3303 | BUILT_IN_MEMSET, BUILT_IN_NORMAL, 1, 0, 0); | |
da9e9f08 | 3304 | built_in_decls[BUILT_IN_STRCMP] = |
fc2aaf30 JM |
3305 | builtin_function_2 ("__builtin_strcmp", "strcmp", |
3306 | int_ftype_cstring_cstring, int_ftype_cstring_cstring, | |
3307 | BUILT_IN_STRCMP, BUILT_IN_NORMAL, 1, 0, 0); | |
3308 | builtin_function_2 ("__builtin_strncmp", "strncmp", | |
3309 | int_ftype_cstring_cstring_sizet, | |
3310 | int_ftype_cstring_cstring_sizet, | |
3311 | BUILT_IN_STRNCMP, BUILT_IN_NORMAL, 1, 0, 0); | |
3312 | builtin_function_2 ("__builtin_strstr", "strstr", | |
3313 | string_ftype_cstring_cstring, string_ftype_cstring_cstring, | |
3314 | BUILT_IN_STRSTR, BUILT_IN_NORMAL, 1, 0, 0); | |
3315 | builtin_function_2 ("__builtin_strpbrk", "strpbrk", | |
3316 | string_ftype_cstring_cstring, string_ftype_cstring_cstring, | |
3317 | BUILT_IN_STRPBRK, BUILT_IN_NORMAL, 1, 0, 0); | |
78e7629e | 3318 | built_in_decls[BUILT_IN_STRCHR] = |
fc2aaf30 JM |
3319 | builtin_function_2 ("__builtin_strchr", "strchr", |
3320 | string_ftype_cstring_int, string_ftype_cstring_int, | |
3321 | BUILT_IN_STRCHR, BUILT_IN_NORMAL, 1, 0, 0); | |
3322 | builtin_function_2 ("__builtin_strrchr", "strrchr", | |
3323 | string_ftype_cstring_int, string_ftype_cstring_int, | |
3324 | BUILT_IN_STRRCHR, BUILT_IN_NORMAL, 1, 0, 0); | |
3325 | builtin_function_2 ("__builtin_strcpy", "strcpy", | |
3326 | string_ftype_string_cstring, string_ftype_string_cstring, | |
3327 | BUILT_IN_STRCPY, BUILT_IN_NORMAL, 1, 0, 0); | |
3328 | builtin_function_2 ("__builtin_strncpy", "strncpy", | |
3329 | string_ftype_string_cstring_sizet, | |
3330 | string_ftype_string_cstring_sizet, | |
3331 | BUILT_IN_STRNCPY, BUILT_IN_NORMAL, 1, 0, 0); | |
d118937d | 3332 | built_in_decls[BUILT_IN_STRCAT] = |
fc2aaf30 JM |
3333 | builtin_function_2 ("__builtin_strcat", "strcat", |
3334 | string_ftype_string_cstring, | |
3335 | string_ftype_string_cstring, | |
3336 | BUILT_IN_STRCAT, BUILT_IN_NORMAL, 1, 0, 0); | |
3337 | builtin_function_2 ("__builtin_strncat", "strncat", | |
3338 | string_ftype_string_cstring_sizet, | |
3339 | string_ftype_string_cstring_sizet, | |
3340 | BUILT_IN_STRNCAT, BUILT_IN_NORMAL, 1, 0, 0); | |
3341 | builtin_function_2 ("__builtin_strspn", "strspn", | |
3342 | sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring, | |
3343 | BUILT_IN_STRSPN, BUILT_IN_NORMAL, 1, 0, 0); | |
3344 | builtin_function_2 ("__builtin_strcspn", "strcspn", | |
3345 | sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring, | |
3346 | BUILT_IN_STRCSPN, BUILT_IN_NORMAL, 1, 0, 0); | |
d118937d | 3347 | built_in_decls[BUILT_IN_STRLEN] = |
fc2aaf30 JM |
3348 | builtin_function_2 ("__builtin_strlen", "strlen", |
3349 | strlen_ftype, strlen_ftype, | |
3350 | BUILT_IN_STRLEN, BUILT_IN_NORMAL, 1, 0, 0); | |
3351 | ||
3352 | builtin_function_2 ("__builtin_sqrtf", "sqrtf", | |
3353 | float_ftype_float, float_ftype_float, | |
3354 | BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0); | |
3355 | builtin_function_2 ("__builtin_fsqrt", "sqrt", | |
3356 | double_ftype_double, double_ftype_double, | |
3357 | BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0); | |
3358 | builtin_function_2 ("__builtin_sqrtl", "sqrtl", | |
3359 | ldouble_ftype_ldouble, ldouble_ftype_ldouble, | |
3360 | BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0); | |
3361 | builtin_function_2 ("__builtin_sinf", "sinf", | |
3362 | float_ftype_float, float_ftype_float, | |
3363 | BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0); | |
3364 | builtin_function_2 ("__builtin_sin", "sin", | |
3365 | double_ftype_double, double_ftype_double, | |
3366 | BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0); | |
3367 | builtin_function_2 ("__builtin_sinl", "sinl", | |
3368 | ldouble_ftype_ldouble, ldouble_ftype_ldouble, | |
3369 | BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0); | |
3370 | builtin_function_2 ("__builtin_cosf", "cosf", | |
3371 | float_ftype_float, float_ftype_float, | |
3372 | BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0); | |
3373 | builtin_function_2 ("__builtin_cos", "cos", | |
3374 | double_ftype_double, double_ftype_double, | |
3375 | BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0); | |
3376 | builtin_function_2 ("__builtin_cosl", "cosl", | |
3377 | ldouble_ftype_ldouble, ldouble_ftype_ldouble, | |
3378 | BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0); | |
341e3d11 JM |
3379 | |
3380 | /* ISO C99 complex arithmetic functions. */ | |
3381 | builtin_function_2 ("__builtin_conjf", "conjf", | |
3382 | cfloat_ftype_cfloat, cfloat_ftype_cfloat, | |
3383 | BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); | |
3384 | builtin_function_2 ("__builtin_conj", "conj", | |
3385 | cdouble_ftype_cdouble, cdouble_ftype_cdouble, | |
3386 | BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); | |
3387 | builtin_function_2 ("__builtin_conjl", "conjl", | |
3388 | cldouble_ftype_cldouble, cldouble_ftype_cldouble, | |
3389 | BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); | |
3390 | builtin_function_2 ("__builtin_crealf", "crealf", | |
3391 | float_ftype_cfloat, float_ftype_cfloat, | |
3392 | BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); | |
3393 | builtin_function_2 ("__builtin_creal", "creal", | |
3394 | double_ftype_cdouble, double_ftype_cdouble, | |
3395 | BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); | |
3396 | builtin_function_2 ("__builtin_creall", "creall", | |
3397 | ldouble_ftype_cldouble, ldouble_ftype_cldouble, | |
3398 | BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); | |
3399 | builtin_function_2 ("__builtin_cimagf", "cimagf", | |
3400 | float_ftype_cfloat, float_ftype_cfloat, | |
3401 | BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); | |
3402 | builtin_function_2 ("__builtin_cimag", "cimag", | |
3403 | double_ftype_cdouble, double_ftype_cdouble, | |
3404 | BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); | |
3405 | builtin_function_2 ("__builtin_cimagl", "cimagl", | |
3406 | ldouble_ftype_cldouble, ldouble_ftype_cldouble, | |
3407 | BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); | |
3408 | ||
ad3fd36f KG |
3409 | built_in_decls[BUILT_IN_PUTCHAR] = |
3410 | builtin_function ("__builtin_putchar", int_ftype_int, | |
3411 | BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar"); | |
3412 | built_in_decls[BUILT_IN_PUTS] = | |
3413 | builtin_function ("__builtin_puts", puts_ftype, | |
3414 | BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts"); | |
fc2aaf30 JM |
3415 | builtin_function_2 ("__builtin_printf", "printf", |
3416 | printf_ftype, printf_ftype, | |
3417 | BUILT_IN_PRINTF, BUILT_IN_FRONTEND, 1, 0, 0); | |
18f988a0 KG |
3418 | builtin_function_2 ("__builtin_fprintf", "fprintf", |
3419 | fprintf_ftype, fprintf_ftype, | |
3420 | BUILT_IN_FPRINTF, BUILT_IN_FRONTEND, 1, 0, 0); | |
07328167 | 3421 | built_in_decls[BUILT_IN_FWRITE] = |
c2e42aab | 3422 | builtin_function ("__builtin_fwrite", fwrite_ftype, |
07328167 | 3423 | BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite"); |
3ff5f682 | 3424 | built_in_decls[BUILT_IN_FPUTC] = |
c2e42aab KG |
3425 | builtin_function ("__builtin_fputc", fputc_ftype, |
3426 | BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc"); | |
3427 | /* Declare the __builtin_ style with arguments and the regular style | |
3428 | without them. We rely on stdio.h to supply the arguments for the | |
3429 | regular style declaration since we had to use void* instead of | |
3430 | FILE* in the __builtin_ prototype supplied here. */ | |
3ff5f682 | 3431 | built_in_decls[BUILT_IN_FPUTS] = |
fc2aaf30 | 3432 | builtin_function_2 ("__builtin_fputs", "fputs", |
c2e42aab | 3433 | fputs_ftype, int_ftype_any, |
fc2aaf30 JM |
3434 | BUILT_IN_FPUTS, BUILT_IN_NORMAL, 1, 0, 0); |
3435 | ||
3436 | /* Declare these functions non-returning | |
3437 | to avoid spurious "control drops through" warnings. */ | |
3438 | builtin_function_2 (NULL_PTR, "abort", | |
3439 | NULL_TREE, ((c_language == clk_cplusplus) | |
3440 | ? void_ftype : void_ftype_any), | |
3441 | 0, NOT_BUILT_IN, 0, 0, 1); | |
3442 | ||
3443 | builtin_function_2 (NULL_PTR, "exit", | |
3444 | NULL_TREE, ((c_language == clk_cplusplus) | |
3445 | ? void_ftype_int : void_ftype_any), | |
3446 | 0, NOT_BUILT_IN, 0, 0, 1); | |
7f4edbcb BS |
3447 | |
3448 | #if 0 | |
3449 | /* Support for these has not been written in either expand_builtin | |
3450 | or build_function_call. */ | |
26db82d8 BS |
3451 | builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, |
3452 | BUILT_IN_NORMAL, NULL_PTR); | |
3453 | builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, | |
3454 | BUILT_IN_NORMAL, NULL_PTR); | |
7f4edbcb | 3455 | builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR, |
26db82d8 | 3456 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3457 | builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL, |
26db82d8 | 3458 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3459 | builtin_function ("__builtin_fmod", double_ftype_double_double, |
26db82d8 | 3460 | BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3461 | builtin_function ("__builtin_frem", double_ftype_double_double, |
26db82d8 | 3462 | BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3463 | builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP, |
26db82d8 | 3464 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3465 | builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN, |
26db82d8 | 3466 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 3467 | #endif |
c530479e | 3468 | |
5b47282c GM |
3469 | main_identifier_node = get_identifier ("main"); |
3470 | ||
c530479e RH |
3471 | /* ??? Perhaps there's a better place to do this. But it is related |
3472 | to __builtin_va_arg, so it isn't that off-the-wall. */ | |
3473 | lang_type_promotes_to = simple_type_promotes_to; | |
7f4edbcb | 3474 | } |
d3707adb RH |
3475 | |
3476 | tree | |
3477 | build_va_arg (expr, type) | |
3478 | tree expr, type; | |
3479 | { | |
3480 | return build1 (VA_ARG_EXPR, type, expr); | |
3481 | } | |
fc2aaf30 JM |
3482 | |
3483 | ||
3484 | /* Possibly define a builtin function with one or two names. BUILTIN_NAME | |
3485 | is an __builtin_-prefixed name; NAME is the ordinary name; one or both | |
3486 | of these may be NULL (though both being NULL is useless). | |
3487 | BUILTIN_TYPE is the type of the __builtin_-prefixed function; | |
3488 | TYPE is the type of the function with the ordinary name. These | |
3489 | may differ if the ordinary name is declared with a looser type to avoid | |
3490 | conflicts with headers. FUNCTION_CODE and CLASS are as for | |
3491 | builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as | |
3492 | the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME. | |
3493 | If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; if | |
3494 | NORETURN_P is nonzero, the function is marked as non-returning. | |
3495 | Returns the declaration of BUILTIN_NAME, if any, otherwise | |
3496 | the declaration of NAME. Does not declare NAME if flag_no_builtin, | |
3497 | or if NONANSI_P and flag_no_nonansi_builtin. */ | |
3498 | ||
3499 | static tree | |
3500 | builtin_function_2 (builtin_name, name, builtin_type, type, function_code, | |
3501 | class, library_name_p, nonansi_p, noreturn_p) | |
3502 | const char *builtin_name; | |
3503 | const char *name; | |
3504 | tree builtin_type; | |
3505 | tree type; | |
3506 | int function_code; | |
3507 | enum built_in_class class; | |
3508 | int library_name_p; | |
3509 | int nonansi_p; | |
3510 | int noreturn_p; | |
3511 | { | |
3512 | tree bdecl = NULL_TREE; | |
3513 | tree decl = NULL_TREE; | |
3514 | if (builtin_name != 0) | |
3515 | { | |
3516 | bdecl = builtin_function (builtin_name, builtin_type, function_code, | |
3517 | class, library_name_p ? name : NULL_PTR); | |
3518 | if (noreturn_p) | |
3519 | { | |
3520 | TREE_THIS_VOLATILE (bdecl) = 1; | |
3521 | TREE_SIDE_EFFECTS (bdecl) = 1; | |
3522 | } | |
3523 | } | |
3524 | if (name != 0 && !flag_no_builtin && !(nonansi_p && flag_no_nonansi_builtin)) | |
3525 | { | |
3526 | decl = builtin_function (name, type, function_code, class, NULL_PTR); | |
3527 | if (nonansi_p) | |
3528 | DECL_BUILT_IN_NONANSI (decl) = 1; | |
3529 | if (noreturn_p) | |
3530 | { | |
3531 | TREE_THIS_VOLATILE (decl) = 1; | |
3532 | TREE_SIDE_EFFECTS (decl) = 1; | |
3533 | } | |
3534 | } | |
3535 | return (bdecl != 0 ? bdecl : decl); | |
3536 | } | |
c530479e RH |
3537 | \f |
3538 | /* Given a type, apply default promotions wrt unnamed function arguments | |
3539 | and return the new type. Return NULL_TREE if no change. */ | |
d125d268 | 3540 | /* ??? There is a function of the same name in the C++ front end that |
c530479e | 3541 | does something similar, but is more thorough and does not return NULL |
d125d268 | 3542 | if no change. We could perhaps share code, but it would make the |
c530479e RH |
3543 | self_promoting_type property harder to identify. */ |
3544 | ||
3545 | tree | |
3546 | simple_type_promotes_to (type) | |
3547 | tree type; | |
3548 | { | |
3549 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
3550 | return double_type_node; | |
3551 | ||
3552 | if (C_PROMOTING_INTEGER_TYPE_P (type)) | |
3553 | { | |
3554 | /* Traditionally, unsignedness is preserved in default promotions. | |
3555 | Also preserve unsignedness if not really getting any wider. */ | |
3556 | if (TREE_UNSIGNED (type) | |
3557 | && (flag_traditional | |
3558 | || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) | |
3559 | return unsigned_type_node; | |
3560 | return integer_type_node; | |
3561 | } | |
3562 | ||
3563 | return NULL_TREE; | |
3564 | } | |
3565 | ||
3566 | /* Return 1 if PARMS specifies a fixed number of parameters | |
3567 | and none of their types is affected by default promotions. */ | |
3568 | ||
3569 | int | |
3570 | self_promoting_args_p (parms) | |
3571 | tree parms; | |
3572 | { | |
3573 | register tree t; | |
3574 | for (t = parms; t; t = TREE_CHAIN (t)) | |
3575 | { | |
3576 | register tree type = TREE_VALUE (t); | |
7e8176d7 | 3577 | |
c530479e RH |
3578 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
3579 | return 0; | |
3580 | ||
3581 | if (type == 0) | |
3582 | return 0; | |
3583 | ||
3584 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
3585 | return 0; | |
3586 | ||
3587 | if (C_PROMOTING_INTEGER_TYPE_P (type)) | |
3588 | return 0; | |
3589 | } | |
3590 | return 1; | |
3591 | } | |
5eda3d66 | 3592 | |
0a7394bc MM |
3593 | /* Recursively examines the array elements of TYPE, until a non-array |
3594 | element type is found. */ | |
3595 | ||
3596 | tree | |
3597 | strip_array_types (type) | |
3598 | tree type; | |
3599 | { | |
3600 | while (TREE_CODE (type) == ARRAY_TYPE) | |
3601 | type = TREE_TYPE (type); | |
3602 | ||
3603 | return type; | |
3604 | } | |
3605 | ||
5eda3d66 RH |
3606 | /* Recognize certain built-in functions so we can make tree-codes |
3607 | other than CALL_EXPR. We do this when it enables fold-const.c | |
3608 | to do something useful. */ | |
3609 | /* ??? By rights this should go in builtins.c, but only C and C++ | |
3610 | implement build_{binary,unary}_op. Not exactly sure what bits | |
3611 | of functionality are actually needed from those functions, or | |
3612 | where the similar functionality exists in the other front ends. */ | |
3613 | ||
3614 | tree | |
3615 | expand_tree_builtin (function, params, coerced_params) | |
3616 | tree function, params, coerced_params; | |
3617 | { | |
3618 | enum tree_code code; | |
3619 | ||
3620 | if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL) | |
3621 | return NULL_TREE; | |
3622 | ||
3623 | switch (DECL_FUNCTION_CODE (function)) | |
3624 | { | |
3625 | case BUILT_IN_ABS: | |
5eda3d66 RH |
3626 | case BUILT_IN_FABS: |
3627 | if (coerced_params == 0) | |
3628 | return integer_zero_node; | |
3629 | return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0); | |
3630 | ||
341e3d11 JM |
3631 | case BUILT_IN_CONJ: |
3632 | if (coerced_params == 0) | |
3633 | return integer_zero_node; | |
3634 | return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0); | |
3635 | ||
3636 | case BUILT_IN_CREAL: | |
3637 | if (coerced_params == 0) | |
3638 | return integer_zero_node; | |
3639 | return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0); | |
3640 | ||
3641 | case BUILT_IN_CIMAG: | |
3642 | if (coerced_params == 0) | |
3643 | return integer_zero_node; | |
3644 | return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0); | |
3645 | ||
5eda3d66 RH |
3646 | case BUILT_IN_ISGREATER: |
3647 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT) | |
3648 | code = UNLE_EXPR; | |
3649 | else | |
3650 | code = LE_EXPR; | |
3651 | goto unordered_cmp; | |
3652 | ||
3653 | case BUILT_IN_ISGREATEREQUAL: | |
3654 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT) | |
3655 | code = UNLT_EXPR; | |
3656 | else | |
3657 | code = LT_EXPR; | |
3658 | goto unordered_cmp; | |
3659 | ||
3660 | case BUILT_IN_ISLESS: | |
3661 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT) | |
3662 | code = UNGE_EXPR; | |
3663 | else | |
3664 | code = GE_EXPR; | |
3665 | goto unordered_cmp; | |
3666 | ||
3667 | case BUILT_IN_ISLESSEQUAL: | |
3668 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT) | |
3669 | code = UNGT_EXPR; | |
3670 | else | |
3671 | code = GT_EXPR; | |
3672 | goto unordered_cmp; | |
3673 | ||
3674 | case BUILT_IN_ISLESSGREATER: | |
3675 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT) | |
3676 | code = UNEQ_EXPR; | |
3677 | else | |
3678 | code = EQ_EXPR; | |
3679 | goto unordered_cmp; | |
3680 | ||
3681 | case BUILT_IN_ISUNORDERED: | |
3682 | if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT) | |
3683 | return integer_zero_node; | |
3684 | code = UNORDERED_EXPR; | |
3685 | goto unordered_cmp; | |
3686 | ||
3687 | unordered_cmp: | |
3688 | { | |
3689 | tree arg0, arg1; | |
3690 | ||
3691 | if (params == 0 | |
3692 | || TREE_CHAIN (params) == 0) | |
3693 | { | |
3694 | error ("too few arguments to function `%s'", | |
3695 | IDENTIFIER_POINTER (DECL_NAME (function))); | |
3696 | return error_mark_node; | |
3697 | } | |
3698 | else if (TREE_CHAIN (TREE_CHAIN (params)) != 0) | |
3699 | { | |
3700 | error ("too many arguments to function `%s'", | |
3701 | IDENTIFIER_POINTER (DECL_NAME (function))); | |
3702 | return error_mark_node; | |
3703 | } | |
3704 | ||
3705 | arg0 = TREE_VALUE (params); | |
3706 | arg1 = TREE_VALUE (TREE_CHAIN (params)); | |
3707 | arg0 = build_binary_op (code, arg0, arg1, 0); | |
3708 | if (code != UNORDERED_EXPR) | |
3709 | arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0); | |
3710 | return arg0; | |
3711 | } | |
3712 | break; | |
3713 | ||
3714 | default: | |
3715 | break; | |
3716 | } | |
3717 | ||
3718 | return NULL_TREE; | |
3719 | } | |
c7d87c0a | 3720 | |
ae499cce MM |
3721 | /* Returns non-zero if CODE is the code for a statement. */ |
3722 | ||
3723 | int | |
3724 | statement_code_p (code) | |
3725 | enum tree_code code; | |
3726 | { | |
3727 | switch (code) | |
3728 | { | |
3729 | case EXPR_STMT: | |
3730 | case COMPOUND_STMT: | |
3731 | case DECL_STMT: | |
3732 | case IF_STMT: | |
3733 | case FOR_STMT: | |
3734 | case WHILE_STMT: | |
3735 | case DO_STMT: | |
3736 | case RETURN_STMT: | |
3737 | case BREAK_STMT: | |
3738 | case CONTINUE_STMT: | |
8f17b5c5 | 3739 | case SCOPE_STMT: |
ae499cce MM |
3740 | case SWITCH_STMT: |
3741 | case GOTO_STMT: | |
3742 | case LABEL_STMT: | |
3743 | case ASM_STMT: | |
3744 | case CASE_LABEL: | |
3745 | return 1; | |
3746 | ||
3747 | default: | |
3748 | if (lang_statement_code_p) | |
3749 | return (*lang_statement_code_p) (code); | |
3750 | return 0; | |
3751 | } | |
3752 | } | |
3753 | ||
86306c8e | 3754 | /* Walk the statement tree, rooted at *tp. Apply FUNC to all the |
ae499cce MM |
3755 | sub-trees of *TP in a pre-order traversal. FUNC is called with the |
3756 | DATA and the address of each sub-tree. If FUNC returns a non-NULL | |
3757 | value, the traversal is aborted, and the value returned by FUNC is | |
3758 | returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of | |
3759 | the node being visited are not walked. | |
3760 | ||
3761 | We don't need a without_duplicates variant of this one because the | |
3762 | statement tree is a tree, not a graph. */ | |
3763 | ||
3764 | tree | |
3765 | walk_stmt_tree (tp, func, data) | |
3766 | tree *tp; | |
3767 | walk_tree_fn func; | |
3768 | void *data; | |
3769 | { | |
3770 | enum tree_code code; | |
3771 | int walk_subtrees; | |
3772 | tree result; | |
3773 | int i, len; | |
3774 | ||
3775 | #define WALK_SUBTREE(NODE) \ | |
3776 | do \ | |
3777 | { \ | |
3778 | result = walk_stmt_tree (&(NODE), func, data); \ | |
3779 | if (result) \ | |
3780 | return result; \ | |
3781 | } \ | |
3782 | while (0) | |
3783 | ||
3784 | /* Skip empty subtrees. */ | |
3785 | if (!*tp) | |
3786 | return NULL_TREE; | |
3787 | ||
3788 | /* Skip subtrees below non-statement nodes. */ | |
3789 | if (!statement_code_p (TREE_CODE (*tp))) | |
3790 | return NULL_TREE; | |
3791 | ||
3792 | /* Call the function. */ | |
3793 | walk_subtrees = 1; | |
3794 | result = (*func) (tp, &walk_subtrees, data); | |
3795 | ||
3796 | /* If we found something, return it. */ | |
3797 | if (result) | |
3798 | return result; | |
3799 | ||
3800 | /* Even if we didn't, FUNC may have decided that there was nothing | |
3801 | interesting below this point in the tree. */ | |
3802 | if (!walk_subtrees) | |
3803 | return NULL_TREE; | |
3804 | ||
3805 | /* FUNC may have modified the tree, recheck that we're looking at a | |
3806 | statement node. */ | |
3807 | code = TREE_CODE (*tp); | |
3808 | if (!statement_code_p (code)) | |
3809 | return NULL_TREE; | |
3810 | ||
3811 | /* Walk over all the sub-trees of this operand. Statement nodes never | |
3812 | contain RTL, and we needn't worry about TARGET_EXPRs. */ | |
3813 | len = TREE_CODE_LENGTH (code); | |
3814 | ||
3815 | /* Go through the subtrees. We need to do this in forward order so | |
3816 | that the scope of a FOR_EXPR is handled properly. */ | |
3817 | for (i = 0; i < len; ++i) | |
3818 | WALK_SUBTREE (TREE_OPERAND (*tp, i)); | |
3819 | ||
3820 | /* Finally visit the chain. This can be tail-recursion optimized if | |
3821 | we write it this way. */ | |
3822 | return walk_stmt_tree (&TREE_CHAIN (*tp), func, data); | |
3823 | ||
3824 | #undef WALK_SUBTREE | |
3825 | } | |
3826 | ||
8f17b5c5 MM |
3827 | /* Used to compare case labels. K1 and K2 are actually tree nodes |
3828 | representing case labels, or NULL_TREE for a `default' label. | |
3829 | Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after | |
3830 | K2, and 0 if K1 and K2 are equal. */ | |
3831 | ||
3832 | int | |
3833 | case_compare (k1, k2) | |
3834 | splay_tree_key k1; | |
3835 | splay_tree_key k2; | |
3836 | { | |
3837 | /* Consider a NULL key (such as arises with a `default' label) to be | |
3838 | smaller than anything else. */ | |
3839 | if (!k1) | |
3840 | return k2 ? -1 : 0; | |
3841 | else if (!k2) | |
3842 | return k1 ? 1 : 0; | |
3843 | ||
3844 | return tree_int_cst_compare ((tree) k1, (tree) k2); | |
3845 | } | |
3846 | ||
3847 | /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If | |
3848 | LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is | |
3849 | actually a `default' label. If only HIGH_VALUE is NULL_TREE, then | |
3850 | case label was declared using the usual C/C++ syntax, rather than | |
3851 | the GNU case range extension. CASES is a tree containing all the | |
3852 | case ranges processed so far; COND is the condition for the | |
3853 | switch-statement itself. Returns the CASE_LABEL created, or | |
3854 | ERROR_MARK_NODE if no CASE_LABEL is created. */ | |
3855 | ||
3856 | tree | |
3857 | c_add_case_label (cases, cond, low_value, high_value) | |
3858 | splay_tree cases; | |
3859 | tree cond; | |
3860 | tree low_value; | |
3861 | tree high_value; | |
3862 | { | |
3863 | tree type; | |
3864 | tree label; | |
3865 | tree case_label; | |
3866 | splay_tree_node node; | |
3867 | ||
3868 | /* Create the LABEL_DECL itself. */ | |
3869 | label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); | |
3870 | DECL_CONTEXT (label) = current_function_decl; | |
3871 | ||
3872 | /* If there was an error processing the switch condition, bail now | |
3873 | before we get more confused. */ | |
3874 | if (!cond || cond == error_mark_node) | |
3875 | { | |
3876 | /* Add a label anyhow so that the back-end doesn't think that | |
3877 | the beginning of the switch is unreachable. */ | |
3878 | if (!cases->root) | |
3879 | add_stmt (build_case_label (NULL_TREE, NULL_TREE, label)); | |
3880 | return error_mark_node; | |
3881 | } | |
3882 | ||
3883 | if ((low_value && TREE_TYPE (low_value) | |
3884 | && POINTER_TYPE_P (TREE_TYPE (low_value))) | |
3885 | || (high_value && TREE_TYPE (high_value) | |
3886 | && POINTER_TYPE_P (TREE_TYPE (high_value)))) | |
3887 | error ("pointers are not permitted as case values"); | |
3888 | ||
3889 | /* Case ranges are a GNU extension. */ | |
3890 | if (high_value && pedantic) | |
3891 | { | |
3892 | if (c_language == clk_cplusplus) | |
3893 | pedwarn ("ISO C++ forbids range expressions in switch statements"); | |
3894 | else | |
3895 | pedwarn ("ISO C forbids range expressions in switch statements"); | |
3896 | } | |
3897 | ||
3898 | type = TREE_TYPE (cond); | |
3899 | if (low_value) | |
3900 | { | |
3901 | low_value = check_case_value (low_value); | |
3902 | low_value = convert_and_check (type, low_value); | |
3903 | } | |
3904 | if (high_value) | |
3905 | { | |
3906 | high_value = check_case_value (high_value); | |
3907 | high_value = convert_and_check (type, high_value); | |
3908 | } | |
3909 | ||
3910 | /* If an error has occurred, bail out now. */ | |
3911 | if (low_value == error_mark_node || high_value == error_mark_node) | |
3912 | { | |
3913 | if (!cases->root) | |
3914 | add_stmt (build_case_label (NULL_TREE, NULL_TREE, label)); | |
3915 | return error_mark_node; | |
3916 | } | |
3917 | ||
3918 | /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't | |
3919 | really a case range, even though it was written that way. Remove | |
3920 | the HIGH_VALUE to simplify later processing. */ | |
3921 | if (tree_int_cst_equal (low_value, high_value)) | |
3922 | high_value = NULL_TREE; | |
3923 | if (low_value && high_value | |
3924 | && !tree_int_cst_lt (low_value, high_value)) | |
3925 | warning ("empty range specified"); | |
3926 | ||
3927 | /* Look up the LOW_VALUE in the table of case labels we already | |
3928 | have. */ | |
3929 | node = splay_tree_lookup (cases, (splay_tree_key) low_value); | |
3930 | /* If there was not an exact match, check for overlapping ranges. | |
3931 | There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; | |
3932 | that's a `default' label and the only overlap is an exact match. */ | |
3933 | if (!node && (low_value || high_value)) | |
3934 | { | |
3935 | splay_tree_node low_bound; | |
3936 | splay_tree_node high_bound; | |
3937 | ||
3938 | /* Even though there wasn't an exact match, there might be an | |
3939 | overlap between this case range and another case range. | |
3940 | Since we've (inductively) not allowed any overlapping case | |
3941 | ranges, we simply need to find the greatest low case label | |
3942 | that is smaller that LOW_VALUE, and the smallest low case | |
3943 | label that is greater than LOW_VALUE. If there is an overlap | |
3944 | it will occur in one of these two ranges. */ | |
3945 | low_bound = splay_tree_predecessor (cases, | |
3946 | (splay_tree_key) low_value); | |
3947 | high_bound = splay_tree_successor (cases, | |
3948 | (splay_tree_key) low_value); | |
3949 | ||
3950 | /* Check to see if the LOW_BOUND overlaps. It is smaller than | |
3951 | the LOW_VALUE, so there is no need to check unless the | |
3952 | LOW_BOUND is in fact itself a case range. */ | |
3953 | if (low_bound | |
3954 | && CASE_HIGH ((tree) low_bound->value) | |
3955 | && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), | |
3956 | low_value) >= 0) | |
3957 | node = low_bound; | |
3958 | /* Check to see if the HIGH_BOUND overlaps. The low end of that | |
3959 | range is bigger than the low end of the current range, so we | |
3960 | are only interested if the current range is a real range, and | |
3961 | not an ordinary case label. */ | |
3962 | else if (high_bound | |
3963 | && high_value | |
3964 | && (tree_int_cst_compare ((tree) high_bound->key, | |
3965 | high_value) | |
3966 | <= 0)) | |
3967 | node = high_bound; | |
3968 | } | |
3969 | /* If there was an overlap, issue an error. */ | |
3970 | if (node) | |
3971 | { | |
3972 | tree duplicate = CASE_LABEL_DECL ((tree) node->value); | |
3973 | ||
3974 | if (high_value) | |
3975 | { | |
3976 | error ("duplicate (or overlapping) case value"); | |
3977 | error_with_decl (duplicate, | |
3978 | "this is the first entry overlapping that value"); | |
3979 | } | |
3980 | else if (low_value) | |
3981 | { | |
3982 | error ("duplicate case value") ; | |
3983 | error_with_decl (duplicate, "previously used here"); | |
3984 | } | |
3985 | else | |
3986 | { | |
3987 | error ("multiple default labels in one switch"); | |
3988 | error_with_decl (duplicate, "this is the first default label"); | |
3989 | } | |
3990 | if (!cases->root) | |
3991 | add_stmt (build_case_label (NULL_TREE, NULL_TREE, label)); | |
3992 | } | |
3993 | ||
3994 | /* Add a CASE_LABEL to the statement-tree. */ | |
3995 | case_label = add_stmt (build_case_label (low_value, high_value, label)); | |
3996 | /* Register this case label in the splay tree. */ | |
3997 | splay_tree_insert (cases, | |
3998 | (splay_tree_key) low_value, | |
3999 | (splay_tree_value) case_label); | |
4000 | ||
4001 | return case_label; | |
4002 | } | |
4003 | ||
4004 | /* Mark P (a stmt_tree) for GC. The use of a `void *' for the | |
4005 | parameter allows this function to be used as a GC-marking | |
4006 | function. */ | |
4007 | ||
4008 | void | |
4009 | mark_stmt_tree (p) | |
4010 | void *p; | |
4011 | { | |
4012 | stmt_tree st = (stmt_tree) p; | |
4013 | ||
4014 | ggc_mark_tree (st->x_last_stmt); | |
4015 | ggc_mark_tree (st->x_last_expr_type); | |
4016 | } | |
4017 | ||
4018 | /* Mark LD for GC. */ | |
4019 | ||
4020 | void | |
4021 | c_mark_lang_decl (c) | |
4022 | struct c_lang_decl *c; | |
4023 | { | |
4024 | ggc_mark_tree (c->saved_tree); | |
4025 | } | |
4026 | ||
4027 | /* Mark F for GC. */ | |
4028 | ||
4029 | void | |
4030 | mark_c_language_function (f) | |
4031 | struct language_function *f; | |
4032 | { | |
4033 | if (!f) | |
4034 | return; | |
4035 | ||
4036 | mark_stmt_tree (&f->x_stmt_tree); | |
4037 | ggc_mark_tree (f->x_scope_stmt_stack); | |
4038 | } | |
4039 | ||
4040 | /* Hook used by expand_expr to expand language-specific tree codes. */ | |
4041 | ||
4042 | rtx | |
4043 | c_expand_expr (exp, target, tmode, modifier) | |
4044 | tree exp; | |
4045 | rtx target; | |
4046 | enum machine_mode tmode; | |
4047 | enum expand_modifier modifier; | |
4048 | { | |
4049 | switch (TREE_CODE (exp)) | |
4050 | { | |
4051 | case STMT_EXPR: | |
4052 | { | |
4053 | tree rtl_expr; | |
4054 | rtx result; | |
4055 | ||
4056 | /* Since expand_expr_stmt calls free_temp_slots after every | |
4057 | expression statement, we must call push_temp_slots here. | |
4058 | Otherwise, any temporaries in use now would be considered | |
4059 | out-of-scope after the first EXPR_STMT from within the | |
4060 | STMT_EXPR. */ | |
4061 | push_temp_slots (); | |
4062 | rtl_expr = expand_start_stmt_expr (); | |
4063 | expand_stmt (STMT_EXPR_STMT (exp)); | |
4064 | expand_end_stmt_expr (rtl_expr); | |
4065 | result = expand_expr (rtl_expr, target, tmode, modifier); | |
4066 | pop_temp_slots (); | |
4067 | return result; | |
4068 | } | |
4069 | break; | |
4070 | ||
c70eaeaf KG |
4071 | case CALL_EXPR: |
4072 | { | |
4073 | if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR | |
4074 | && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) | |
4075 | == FUNCTION_DECL) | |
4076 | && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) | |
4077 | && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) | |
4078 | == BUILT_IN_FRONTEND)) | |
4079 | return c_expand_builtin (exp, target, tmode, modifier); | |
4080 | else | |
4081 | abort(); | |
4082 | } | |
4083 | break; | |
4084 | ||
8f17b5c5 MM |
4085 | default: |
4086 | abort (); | |
4087 | } | |
4088 | ||
4089 | abort (); | |
4090 | return NULL; | |
4091 | } | |
4092 | ||
4093 | /* Hook used by safe_from_p to handle language-specific tree codes. */ | |
4094 | ||
4095 | int | |
4096 | c_safe_from_p (target, exp) | |
4097 | rtx target; | |
4098 | tree exp; | |
4099 | { | |
4100 | /* We can see statements here when processing the body of a | |
4101 | statement-expression. For a declaration statement declaring a | |
4102 | variable, look at the variable's initializer. */ | |
4103 | if (TREE_CODE (exp) == DECL_STMT) | |
4104 | { | |
4105 | tree decl = DECL_STMT_DECL (exp); | |
4106 | ||
4107 | if (TREE_CODE (decl) == VAR_DECL | |
4108 | && DECL_INITIAL (decl) | |
4109 | && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0)) | |
4110 | return 0; | |
4111 | } | |
4112 | ||
4113 | /* For any statement, we must follow the statement-chain. */ | |
4114 | if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp)) | |
4115 | return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0); | |
4116 | ||
4117 | /* Assume everything else is safe. */ | |
4118 | return 1; | |
4119 | } | |
4120 | ||
3fe30ff6 RH |
4121 | /* Hook used by unsafe_for_reeval to handle language-specific tree codes. */ |
4122 | ||
4123 | int | |
4124 | c_unsafe_for_reeval (exp) | |
4125 | tree exp; | |
4126 | { | |
4127 | /* Statement expressions may not be reevaluated. */ | |
4128 | if (TREE_CODE (exp) == STMT_EXPR) | |
4129 | return 2; | |
4130 | ||
4131 | /* Walk all other expressions. */ | |
4132 | return -1; | |
4133 | } | |
4134 | ||
c7d87c0a BC |
4135 | /* Tree code classes. */ |
4136 | ||
4137 | #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE, | |
4138 | ||
4139 | static char c_tree_code_type[] = { | |
4140 | 'x', | |
4141 | #include "c-common.def" | |
4142 | }; | |
4143 | #undef DEFTREECODE | |
4144 | ||
4145 | /* Table indexed by tree code giving number of expression | |
4146 | operands beyond the fixed part of the node structure. | |
4147 | Not used for types or decls. */ | |
4148 | ||
4149 | #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH, | |
4150 | ||
4151 | static int c_tree_code_length[] = { | |
4152 | 0, | |
4153 | #include "c-common.def" | |
4154 | }; | |
4155 | #undef DEFTREECODE | |
4156 | ||
4157 | /* Names of tree components. | |
4158 | Used for printing out the tree and error messages. */ | |
4159 | #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME, | |
4160 | ||
4161 | static const char *c_tree_code_name[] = { | |
4162 | "@@dummy", | |
4163 | #include "c-common.def" | |
4164 | }; | |
4165 | #undef DEFTREECODE | |
4166 | ||
4167 | /* Adds the tree codes specific to the C front end to the list of all | |
4168 | tree codes. */ | |
4169 | ||
4170 | void | |
5538e30f | 4171 | add_c_tree_codes () |
c7d87c0a BC |
4172 | { |
4173 | memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE, | |
4174 | c_tree_code_type, | |
4175 | (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE); | |
4176 | memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE, | |
4177 | c_tree_code_length, | |
4178 | (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int)); | |
4179 | memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE, | |
4180 | c_tree_code_name, | |
4181 | (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *)); | |
3fe30ff6 | 4182 | lang_unsafe_for_reeval = c_unsafe_for_reeval; |
c7d87c0a | 4183 | } |
c70eaeaf KG |
4184 | |
4185 | #define CALLED_AS_BUILT_IN(NODE) \ | |
4186 | (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10)) | |
4187 | ||
4188 | static rtx | |
4189 | c_expand_builtin (exp, target, tmode, modifier) | |
4190 | tree exp; | |
4191 | rtx target; | |
4192 | enum machine_mode tmode; | |
4193 | enum expand_modifier modifier; | |
4194 | { | |
4195 | tree type = TREE_TYPE (exp); | |
4196 | tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); | |
4197 | tree arglist = TREE_OPERAND (exp, 1); | |
4198 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
4199 | enum tree_code code = TREE_CODE (exp); | |
4200 | const int ignore = (target == const0_rtx | |
4201 | || ((code == NON_LVALUE_EXPR || code == NOP_EXPR | |
4202 | || code == CONVERT_EXPR || code == REFERENCE_EXPR | |
4203 | || code == COND_EXPR) | |
4204 | && TREE_CODE (type) == VOID_TYPE)); | |
4205 | ||
4206 | if (! optimize && ! CALLED_AS_BUILT_IN (fndecl)) | |
4207 | return expand_call (exp, target, ignore); | |
4208 | ||
4209 | switch (fcode) | |
4210 | { | |
4211 | case BUILT_IN_PRINTF: | |
4212 | target = c_expand_builtin_printf (arglist, target, tmode, | |
4213 | modifier, ignore); | |
4214 | if (target) | |
4215 | return target; | |
4216 | break; | |
4217 | ||
18f988a0 KG |
4218 | case BUILT_IN_FPRINTF: |
4219 | target = c_expand_builtin_fprintf (arglist, target, tmode, | |
4220 | modifier, ignore); | |
4221 | if (target) | |
4222 | return target; | |
4223 | break; | |
4224 | ||
c70eaeaf KG |
4225 | default: /* just do library call, if unknown builtin */ |
4226 | error ("built-in function `%s' not currently supported", | |
4227 | IDENTIFIER_POINTER (DECL_NAME (fndecl))); | |
4228 | } | |
4229 | ||
4230 | /* The switch statement above can drop through to cause the function | |
4231 | to be called normally. */ | |
4232 | return expand_call (exp, target, ignore); | |
4233 | } | |
4234 | ||
4235 | /* Check an arglist to *printf for problems. The arglist should start | |
4236 | at the format specifier, with the remaining arguments immediately | |
4237 | following it. */ | |
4238 | static int | |
4239 | is_valid_printf_arglist (arglist) | |
4240 | tree arglist; | |
4241 | { | |
4242 | /* Save this value so we can restore it later. */ | |
4243 | const int SAVE_pedantic = pedantic; | |
4244 | int diagnostic_occurred = 0; | |
4245 | ||
4246 | /* Set this to a known value so the user setting won't affect code | |
4247 | generation. */ | |
4248 | pedantic = 1; | |
4249 | /* Check to make sure there are no format specifier errors. */ | |
4250 | check_function_format (&diagnostic_occurred, | |
4251 | maybe_get_identifier("printf"), | |
4252 | NULL_TREE, arglist); | |
4253 | ||
4254 | /* Restore the value of `pedantic'. */ | |
4255 | pedantic = SAVE_pedantic; | |
4256 | ||
4257 | /* If calling `check_function_format_ptr' produces a warning, we | |
4258 | return false, otherwise we return true. */ | |
4259 | return ! diagnostic_occurred; | |
4260 | } | |
4261 | ||
4262 | /* If the arguments passed to printf are suitable for optimizations, | |
4263 | we attempt to transform the call. */ | |
4264 | static rtx | |
4265 | c_expand_builtin_printf (arglist, target, tmode, modifier, ignore) | |
4266 | tree arglist; | |
4267 | rtx target; | |
4268 | enum machine_mode tmode; | |
4269 | enum expand_modifier modifier; | |
4270 | int ignore; | |
4271 | { | |
4272 | tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR], | |
4273 | fn_puts = built_in_decls[BUILT_IN_PUTS]; | |
4274 | tree fn, format_arg, stripped_string; | |
4275 | ||
4276 | /* If the return value is used, or the replacement _DECL isn't | |
4277 | initialized, don't do the transformation. */ | |
4278 | if (!ignore || !fn_putchar || !fn_puts) | |
4279 | return 0; | |
4280 | ||
4281 | /* Verify the required arguments in the original call. */ | |
4282 | if (arglist == 0 | |
4283 | || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)) | |
4284 | return 0; | |
4285 | ||
4286 | /* Check the specifier vs. the parameters. */ | |
4287 | if (!is_valid_printf_arglist (arglist)) | |
4288 | return 0; | |
4289 | ||
4290 | format_arg = TREE_VALUE (arglist); | |
4291 | stripped_string = format_arg; | |
4292 | STRIP_NOPS (stripped_string); | |
4293 | if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR) | |
4294 | stripped_string = TREE_OPERAND (stripped_string, 0); | |
4295 | ||
4296 | /* If the format specifier isn't a STRING_CST, punt. */ | |
4297 | if (TREE_CODE (stripped_string) != STRING_CST) | |
4298 | return 0; | |
4299 | ||
4300 | /* OK! We can attempt optimization. */ | |
4301 | ||
4302 | /* If the format specifier was "%s\n", call __builtin_puts(arg2). */ | |
4303 | if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0) | |
4304 | { | |
4305 | arglist = TREE_CHAIN (arglist); | |
4306 | fn = fn_puts; | |
4307 | } | |
4308 | /* If the format specifier was "%c", call __builtin_putchar (arg2). */ | |
4309 | else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0) | |
4310 | { | |
4311 | arglist = TREE_CHAIN (arglist); | |
4312 | fn = fn_putchar; | |
4313 | } | |
4314 | else | |
4315 | { | |
4316 | /* We can't handle anything else with % args or %% ... yet. */ | |
4317 | if (strchr (TREE_STRING_POINTER (stripped_string), '%')) | |
4318 | return 0; | |
4319 | ||
4320 | /* If the resulting constant string has a length of 1, call | |
4321 | putchar. Note, TREE_STRING_LENGTH includes the terminating | |
4322 | NULL in its count. */ | |
4323 | if (TREE_STRING_LENGTH (stripped_string) == 2) | |
4324 | { | |
4325 | /* Given printf("c"), (where c is any one character,) | |
4326 | convert "c"[0] to an int and pass that to the replacement | |
4327 | function. */ | |
4328 | arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0); | |
4329 | arglist = build_tree_list (NULL_TREE, arglist); | |
4330 | ||
4331 | fn = fn_putchar; | |
4332 | } | |
4333 | /* If the resulting constant was "string\n", call | |
4334 | __builtin_puts("string"). Ensure "string" has at least one | |
4335 | character besides the trailing \n. Note, TREE_STRING_LENGTH | |
4336 | includes the terminating NULL in its count. */ | |
4337 | else if (TREE_STRING_LENGTH (stripped_string) > 2 | |
4338 | && TREE_STRING_POINTER (stripped_string) | |
4339 | [TREE_STRING_LENGTH (stripped_string) - 2] == '\n') | |
4340 | { | |
4341 | /* Create a NULL-terminated string that's one char shorter | |
4342 | than the original, stripping off the trailing '\n'. */ | |
4343 | const int newlen = TREE_STRING_LENGTH (stripped_string) - 1; | |
4344 | char *newstr = (char *) alloca (newlen); | |
4345 | memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1); | |
4346 | newstr[newlen - 1] = 0; | |
4347 | ||
1092710d | 4348 | arglist = combine_strings (build_string (newlen, newstr)); |
c70eaeaf KG |
4349 | arglist = build_tree_list (NULL_TREE, arglist); |
4350 | fn = fn_puts; | |
4351 | } | |
4352 | else | |
4353 | /* We'd like to arrange to call fputs(string) here, but we | |
4354 | need stdout and don't have a way to get it ... yet. */ | |
4355 | return 0; | |
4356 | } | |
4357 | ||
4358 | return expand_expr (build_function_call (fn, arglist), | |
4359 | (ignore ? const0_rtx : target), | |
4360 | tmode, modifier); | |
4361 | } | |
18f988a0 KG |
4362 | |
4363 | /* If the arguments passed to fprintf are suitable for optimizations, | |
4364 | we attempt to transform the call. */ | |
4365 | static rtx | |
4366 | c_expand_builtin_fprintf (arglist, target, tmode, modifier, ignore) | |
4367 | tree arglist; | |
4368 | rtx target; | |
4369 | enum machine_mode tmode; | |
4370 | enum expand_modifier modifier; | |
4371 | int ignore; | |
4372 | { | |
4373 | tree fn_fputc = built_in_decls[BUILT_IN_FPUTC], | |
4374 | fn_fputs = built_in_decls[BUILT_IN_FPUTS]; | |
4375 | tree fn, format_arg, stripped_string; | |
4376 | ||
4377 | /* If the return value is used, or the replacement _DECL isn't | |
4378 | initialized, don't do the transformation. */ | |
4379 | if (!ignore || !fn_fputc || !fn_fputs) | |
4380 | return 0; | |
4381 | ||
4382 | /* Verify the required arguments in the original call. */ | |
4383 | if (arglist == 0 | |
4384 | || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE) | |
4385 | || (TREE_CHAIN (arglist) == 0) | |
4386 | || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) != | |
4387 | POINTER_TYPE)) | |
4388 | return 0; | |
4389 | ||
4390 | /* Check the specifier vs. the parameters. */ | |
4391 | if (!is_valid_printf_arglist (TREE_CHAIN (arglist))) | |
4392 | return 0; | |
4393 | ||
4394 | format_arg = TREE_VALUE (TREE_CHAIN (arglist)); | |
4395 | stripped_string = format_arg; | |
4396 | STRIP_NOPS (stripped_string); | |
4397 | if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR) | |
4398 | stripped_string = TREE_OPERAND (stripped_string, 0); | |
4399 | ||
4400 | /* If the format specifier isn't a STRING_CST, punt. */ | |
4401 | if (TREE_CODE (stripped_string) != STRING_CST) | |
4402 | return 0; | |
4403 | ||
4404 | /* OK! We can attempt optimization. */ | |
4405 | ||
4406 | /* If the format specifier was "%s", call __builtin_fputs(arg3, arg1). */ | |
4407 | if (strcmp (TREE_STRING_POINTER (stripped_string), "%s") == 0) | |
4408 | { | |
4409 | tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist)); | |
4410 | arglist = tree_cons (NULL_TREE, | |
4411 | TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))), | |
4412 | newarglist); | |
4413 | fn = fn_fputs; | |
4414 | } | |
4415 | /* If the format specifier was "%c", call __builtin_fputc (arg3, arg1). */ | |
4416 | else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0) | |
4417 | { | |
4418 | tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist)); | |
4419 | arglist = tree_cons (NULL_TREE, | |
4420 | TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))), | |
4421 | newarglist); | |
4422 | fn = fn_fputc; | |
4423 | } | |
4424 | else | |
4425 | { | |
4426 | /* We can't handle anything else with % args or %% ... yet. */ | |
4427 | if (strchr (TREE_STRING_POINTER (stripped_string), '%')) | |
4428 | return 0; | |
4429 | ||
4430 | /* When "string" doesn't contain %, replace all cases of | |
4431 | fprintf(stream,string) with fputs(string,stream). The fputs | |
4432 | builtin will take take of special cases like length==1. */ | |
4433 | arglist = tree_cons (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)), | |
4434 | build_tree_list (NULL_TREE, TREE_VALUE (arglist))); | |
4435 | fn = fn_fputs; | |
4436 | } | |
4437 | ||
4438 | return expand_expr (build_function_call (fn, arglist), | |
4439 | (ignore ? const0_rtx : target), | |
4440 | tmode, modifier); | |
4441 | } | |
19552aa5 JM |
4442 | \f |
4443 | ||
4444 | /* Given a boolean expression ARG, return a tree representing an increment | |
4445 | or decrement (as indicated by CODE) of ARG. The front end must check for | |
4446 | invalid cases (e.g., decrement in C++). */ | |
4447 | tree | |
4448 | boolean_increment (code, arg) | |
4449 | enum tree_code code; | |
4450 | tree arg; | |
4451 | { | |
4452 | tree val; | |
4453 | tree true_res = (c_language == clk_cplusplus | |
4454 | ? boolean_true_node | |
4455 | : c_bool_true_node); | |
4456 | arg = stabilize_reference (arg); | |
4457 | switch (code) | |
4458 | { | |
4459 | case PREINCREMENT_EXPR: | |
4460 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); | |
4461 | break; | |
4462 | case POSTINCREMENT_EXPR: | |
4463 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); | |
4464 | arg = save_expr (arg); | |
4465 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); | |
4466 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4467 | break; | |
4468 | case PREDECREMENT_EXPR: | |
4469 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg)); | |
4470 | break; | |
4471 | case POSTDECREMENT_EXPR: | |
4472 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg)); | |
4473 | arg = save_expr (arg); | |
4474 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); | |
4475 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4476 | break; | |
4477 | default: | |
4478 | abort (); | |
4479 | } | |
4480 | TREE_SIDE_EFFECTS (val) = 1; | |
4481 | return val; | |
4482 | } | |
03dc0325 JM |
4483 | \f |
4484 | ||
4485 | /* Do the parts of lang_init common to C and C++. */ | |
4486 | void | |
4487 | c_common_lang_init () | |
4488 | { | |
4489 | /* If still "unspecified", make it match -fbounded-pointers. */ | |
4490 | if (flag_bounds_check < 0) | |
4491 | flag_bounds_check = flag_bounded_pointers; | |
4492 | ||
4493 | /* Special format checking options don't work without -Wformat; warn if | |
4494 | they are used. */ | |
4495 | if (warn_format_y2k && !warn_format) | |
4496 | warning ("-Wformat-y2k ignored without -Wformat"); | |
4497 | if (warn_format_extra_args && !warn_format) | |
4498 | warning ("-Wformat-extra-args ignored without -Wformat"); | |
4499 | if (warn_format_nonliteral && !warn_format) | |
4500 | warning ("-Wformat-nonliteral ignored without -Wformat"); | |
4501 | if (warn_format_security && !warn_format) | |
4502 | warning ("-Wformat-security ignored without -Wformat"); | |
4503 | if (warn_missing_format_attribute && !warn_format) | |
4504 | warning ("-Wmissing-format-attribute ignored without -Wformat"); | |
4505 | } |