]>
Commit | Line | Data |
---|---|---|
b0fc3e72 | 1 | /* Subroutines shared by all languages that are variants of C. |
d513ec2f | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
fd6f6435 | 3 | 2001, 2002, 2003 Free Software Foundation, Inc. |
b0fc3e72 | 4 | |
f12b58b3 | 5 | This file is part of GCC. |
b0fc3e72 | 6 | |
f12b58b3 | 7 | GCC is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 2, or (at your option) any later | |
10 | version. | |
b0fc3e72 | 11 | |
f12b58b3 | 12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
b0fc3e72 | 16 | |
17 | You should have received a copy of the GNU General Public License | |
f12b58b3 | 18 | along with GCC; see the file COPYING. If not, write to the Free |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
b0fc3e72 | 21 | |
22 | #include "config.h" | |
405711de | 23 | #include "system.h" |
805e22b2 | 24 | #include "coretypes.h" |
25 | #include "tm.h" | |
e48d0f41 | 26 | #include "intl.h" |
b0fc3e72 | 27 | #include "tree.h" |
b0fc3e72 | 28 | #include "flags.h" |
cd03a192 | 29 | #include "output.h" |
a3fa7feb | 30 | #include "c-pragma.h" |
a5b1863e | 31 | #include "rtl.h" |
dc12af01 | 32 | #include "ggc.h" |
573aba85 | 33 | #include "varray.h" |
74647769 | 34 | #include "expr.h" |
e41f0d80 | 35 | #include "c-common.h" |
cdc9fa3e | 36 | #include "diagnostic.h" |
d8c9779c | 37 | #include "tm_p.h" |
4e91a871 | 38 | #include "obstack.h" |
a654e028 | 39 | #include "cpplib.h" |
8ee295a7 | 40 | #include "target.h" |
96554925 | 41 | #include "langhooks.h" |
f3dde807 | 42 | #include "tree-inline.h" |
7acb29a3 | 43 | #include "c-tree.h" |
69579044 | 44 | #include "toplev.h" |
fd6f6435 | 45 | |
90cc7820 | 46 | cpp_reader *parse_in; /* Declared in c-pragma.h. */ |
a654e028 | 47 | |
174fcc61 | 48 | /* We let tm.h override the types used here, to handle trivial differences |
49 | such as the choice of unsigned int or long unsigned int for size_t. | |
50 | When machines start needing nontrivial differences in the size type, | |
51 | it would be best to do something here to figure out automatically | |
52 | from other information what type to use. */ | |
53 | ||
54 | #ifndef SIZE_TYPE | |
55 | #define SIZE_TYPE "long unsigned int" | |
56 | #endif | |
57 | ||
58 | #ifndef WCHAR_TYPE | |
59 | #define WCHAR_TYPE "int" | |
60 | #endif | |
61 | ||
18ef7ac2 | 62 | /* WCHAR_TYPE gets overridden by -fshort-wchar. */ |
63 | #define MODIFIED_WCHAR_TYPE \ | |
64 | (flag_short_wchar ? "short unsigned int" : WCHAR_TYPE) | |
65 | ||
194c4d9f | 66 | #ifndef PTRDIFF_TYPE |
67 | #define PTRDIFF_TYPE "long int" | |
68 | #endif | |
69 | ||
6bf5ed8d | 70 | #ifndef WINT_TYPE |
71 | #define WINT_TYPE "unsigned int" | |
72 | #endif | |
73 | ||
74 | #ifndef INTMAX_TYPE | |
75 | #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
76 | ? "int" \ | |
77 | : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
78 | ? "long int" \ | |
79 | : "long long int")) | |
80 | #endif | |
81 | ||
82 | #ifndef UINTMAX_TYPE | |
83 | #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
84 | ? "unsigned int" \ | |
85 | : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
86 | ? "long unsigned int" \ | |
87 | : "long long unsigned int")) | |
88 | #endif | |
89 | ||
72040e7e | 90 | /* The following symbols are subsumed in the c_global_trees array, and |
44e9fa65 | 91 | listed here individually for documentation purposes. |
72040e7e | 92 | |
93 | INTEGER_TYPE and REAL_TYPE nodes for the standard data types. | |
94 | ||
95 | tree short_integer_type_node; | |
96 | tree long_integer_type_node; | |
97 | tree long_long_integer_type_node; | |
98 | ||
99 | tree short_unsigned_type_node; | |
100 | tree long_unsigned_type_node; | |
101 | tree long_long_unsigned_type_node; | |
102 | ||
3c2239cf | 103 | tree truthvalue_type_node; |
104 | tree truthvalue_false_node; | |
105 | tree truthvalue_true_node; | |
72040e7e | 106 | |
107 | tree ptrdiff_type_node; | |
108 | ||
109 | tree unsigned_char_type_node; | |
110 | tree signed_char_type_node; | |
111 | tree wchar_type_node; | |
112 | tree signed_wchar_type_node; | |
113 | tree unsigned_wchar_type_node; | |
114 | ||
115 | tree float_type_node; | |
116 | tree double_type_node; | |
117 | tree long_double_type_node; | |
118 | ||
119 | tree complex_integer_type_node; | |
120 | tree complex_float_type_node; | |
121 | tree complex_double_type_node; | |
122 | tree complex_long_double_type_node; | |
123 | ||
124 | tree intQI_type_node; | |
125 | tree intHI_type_node; | |
126 | tree intSI_type_node; | |
127 | tree intDI_type_node; | |
128 | tree intTI_type_node; | |
129 | ||
130 | tree unsigned_intQI_type_node; | |
131 | tree unsigned_intHI_type_node; | |
132 | tree unsigned_intSI_type_node; | |
133 | tree unsigned_intDI_type_node; | |
134 | tree unsigned_intTI_type_node; | |
135 | ||
136 | tree widest_integer_literal_type_node; | |
137 | tree widest_unsigned_literal_type_node; | |
138 | ||
139 | Nodes for types `void *' and `const void *'. | |
140 | ||
141 | tree ptr_type_node, const_ptr_type_node; | |
142 | ||
143 | Nodes for types `char *' and `const char *'. | |
144 | ||
145 | tree string_type_node, const_string_type_node; | |
146 | ||
147 | Type `char[SOMENUMBER]'. | |
148 | Used when an array of char is needed and the size is irrelevant. | |
149 | ||
150 | tree char_array_type_node; | |
151 | ||
152 | Type `int[SOMENUMBER]' or something like it. | |
153 | Used when an array of int needed and the size is irrelevant. | |
154 | ||
155 | tree int_array_type_node; | |
156 | ||
157 | Type `wchar_t[SOMENUMBER]' or something like it. | |
158 | Used when a wide string literal is created. | |
159 | ||
160 | tree wchar_array_type_node; | |
161 | ||
162 | Type `int ()' -- used for implicit declaration of functions. | |
163 | ||
164 | tree default_function_type; | |
165 | ||
72040e7e | 166 | A VOID_TYPE node, packaged in a TREE_LIST. |
167 | ||
168 | tree void_list_node; | |
169 | ||
734c98be | 170 | The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__, |
65b7f83f | 171 | and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__ |
172 | VAR_DECLS, but C++ does.) | |
71d9fc9b | 173 | |
65b7f83f | 174 | tree function_name_decl_node; |
734c98be | 175 | tree pretty_function_name_decl_node; |
65b7f83f | 176 | tree c99_function_name_decl_node; |
177 | ||
178 | Stack of nested function name VAR_DECLs. | |
1cae46be | 179 | |
65b7f83f | 180 | tree saved_function_name_decls; |
71d9fc9b | 181 | |
72040e7e | 182 | */ |
183 | ||
184 | tree c_global_trees[CTI_MAX]; | |
ceee5ef4 | 185 | |
bac62436 | 186 | /* TRUE if a code represents a statement. The front end init |
187 | langhook should take care of initialization of this array. */ | |
188 | ||
189 | bool statement_code_p[MAX_TREE_CODES]; | |
573aba85 | 190 | \f |
574a6990 | 191 | /* Switches common to the C front ends. */ |
192 | ||
ffd56b21 | 193 | /* Nonzero if prepreprocessing only. */ |
f7070933 | 194 | |
ffd56b21 | 195 | int flag_preprocess_only; |
196 | ||
f7070933 | 197 | /* Nonzero means don't output line number information. */ |
198 | ||
199 | char flag_no_line_commands; | |
200 | ||
201 | /* Nonzero causes -E output not to be done, but directives such as | |
202 | #define that have side effects are still obeyed. */ | |
203 | ||
204 | char flag_no_output; | |
205 | ||
206 | /* Nonzero means dump macros in some fashion. */ | |
207 | ||
208 | char flag_dump_macros; | |
209 | ||
210 | /* Nonzero means pass #include lines through to the output. */ | |
211 | ||
212 | char flag_dump_includes; | |
213 | ||
573aba85 | 214 | /* The file name to which we should write a precompiled header, or |
215 | NULL if no header will be written in this compile. */ | |
216 | ||
217 | const char *pch_file; | |
218 | ||
1ed9d5f5 | 219 | /* Nonzero if an ISO standard was selected. It rejects macros in the |
220 | user's namespace. */ | |
221 | int flag_iso; | |
222 | ||
223 | /* Nonzero if -undef was given. It suppresses target built-in macros | |
224 | and assertions. */ | |
225 | int flag_undef; | |
226 | ||
0270ae90 | 227 | /* Nonzero means don't recognize the non-ANSI builtin functions. */ |
228 | ||
229 | int flag_no_builtin; | |
230 | ||
231 | /* Nonzero means don't recognize the non-ANSI builtin functions. | |
232 | -ansi sets this. */ | |
233 | ||
234 | int flag_no_nonansi_builtin; | |
235 | ||
174fcc61 | 236 | /* Nonzero means give `double' the same size as `float'. */ |
237 | ||
238 | int flag_short_double; | |
239 | ||
240 | /* Nonzero means give `wchar_t' the same size as `short'. */ | |
241 | ||
242 | int flag_short_wchar; | |
243 | ||
07ebd091 | 244 | /* Nonzero means allow Microsoft extensions without warnings or errors. */ |
245 | int flag_ms_extensions; | |
246 | ||
574a6990 | 247 | /* Nonzero means don't recognize the keyword `asm'. */ |
248 | ||
249 | int flag_no_asm; | |
250 | ||
251 | /* Nonzero means give string constants the type `const char *', as mandated | |
252 | by the standard. */ | |
253 | ||
254 | int flag_const_strings; | |
255 | ||
574a6990 | 256 | /* Nonzero means to treat bitfields as signed unless they say `unsigned'. */ |
257 | ||
258 | int flag_signed_bitfields = 1; | |
259 | int explicit_flag_signed_bitfields; | |
260 | ||
261 | /* Nonzero means warn about pointer casts that can drop a type qualifier | |
262 | from the pointer target type. */ | |
263 | ||
264 | int warn_cast_qual; | |
265 | ||
266 | /* Warn about functions which might be candidates for format attributes. */ | |
267 | ||
268 | int warn_missing_format_attribute; | |
269 | ||
270 | /* Nonzero means warn about sizeof(function) or addition/subtraction | |
271 | of function pointers. */ | |
272 | ||
273 | int warn_pointer_arith; | |
274 | ||
275 | /* Nonzero means warn for any global function def | |
276 | without separate previous prototype decl. */ | |
277 | ||
278 | int warn_missing_prototypes; | |
279 | ||
280 | /* Warn if adding () is suggested. */ | |
281 | ||
282 | int warn_parentheses; | |
283 | ||
284 | /* Warn if initializer is not completely bracketed. */ | |
285 | ||
286 | int warn_missing_braces; | |
287 | ||
288 | /* Warn about comparison of signed and unsigned values. | |
5125036e | 289 | If -1, neither -Wsign-compare nor -Wno-sign-compare has been specified |
290 | (in which case -Wextra gets to decide). */ | |
574a6990 | 291 | |
5125036e | 292 | int warn_sign_compare = -1; |
574a6990 | 293 | |
294 | /* Nonzero means warn about usage of long long when `-pedantic'. */ | |
295 | ||
296 | int warn_long_long = 1; | |
297 | ||
298 | /* Nonzero means warn about deprecated conversion from string constant to | |
299 | `char *'. */ | |
300 | ||
301 | int warn_write_strings; | |
302 | ||
303 | /* Nonzero means warn about multiple (redundant) decls for the same single | |
304 | variable or function. */ | |
305 | ||
306 | int warn_redundant_decls; | |
307 | ||
7299020b | 308 | /* Warn about testing equality of floating point numbers. */ |
574a6990 | 309 | |
310 | int warn_float_equal; | |
311 | ||
312 | /* Warn about a subscript that has type char. */ | |
313 | ||
314 | int warn_char_subscripts; | |
315 | ||
316 | /* Warn if a type conversion is done that might have confusing results. */ | |
317 | ||
318 | int warn_conversion; | |
319 | ||
1cae46be | 320 | /* Warn about #pragma directives that are not recognized. */ |
574a6990 | 321 | |
1cae46be | 322 | int warn_unknown_pragmas; /* Tri state variable. */ |
574a6990 | 323 | |
574a6990 | 324 | /* Warn about format/argument anomalies in calls to formatted I/O functions |
325 | (*printf, *scanf, strftime, strfmon, etc.). */ | |
326 | ||
327 | int warn_format; | |
328 | ||
329 | /* Warn about Y2K problems with strftime formats. */ | |
330 | ||
331 | int warn_format_y2k; | |
332 | ||
333 | /* Warn about excess arguments to formats. */ | |
334 | ||
335 | int warn_format_extra_args; | |
336 | ||
337 | /* Warn about zero-length formats. */ | |
338 | ||
339 | int warn_format_zero_length; | |
340 | ||
341 | /* Warn about non-literal format arguments. */ | |
342 | ||
343 | int warn_format_nonliteral; | |
344 | ||
345 | /* Warn about possible security problems with calls to format functions. */ | |
346 | ||
347 | int warn_format_security; | |
348 | ||
349 | ||
350 | /* C/ObjC language option variables. */ | |
351 | ||
352 | ||
353 | /* Nonzero means message about use of implicit function declarations; | |
354 | 1 means warning; 2 means error. */ | |
355 | ||
356 | int mesg_implicit_function_declaration = -1; | |
357 | ||
358 | /* Nonzero means allow type mismatches in conditional expressions; | |
359 | just make their values `void'. */ | |
360 | ||
361 | int flag_cond_mismatch; | |
362 | ||
363 | /* Nonzero means enable C89 Amendment 1 features. */ | |
364 | ||
365 | int flag_isoc94; | |
366 | ||
367 | /* Nonzero means use the ISO C99 dialect of C. */ | |
368 | ||
369 | int flag_isoc99; | |
370 | ||
371 | /* Nonzero means that we have builtin functions, and main is an int */ | |
372 | ||
373 | int flag_hosted = 1; | |
374 | ||
574a6990 | 375 | /* Nonzero means warn when casting a function call to a type that does |
376 | not match the return type (e.g. (float)sqrt() or (anything*)malloc() | |
377 | when there is no previous declaration of sqrt or malloc. */ | |
378 | ||
379 | int warn_bad_function_cast; | |
380 | ||
381 | /* Warn about traditional constructs whose meanings changed in ANSI C. */ | |
382 | ||
383 | int warn_traditional; | |
384 | ||
aa267cf6 | 385 | /* Nonzero means warn for a declaration found after a statement. */ |
386 | ||
387 | int warn_declaration_after_statement; | |
388 | ||
574a6990 | 389 | /* Nonzero means warn for non-prototype function decls |
390 | or non-prototyped defs without previous prototype. */ | |
391 | ||
392 | int warn_strict_prototypes; | |
393 | ||
394 | /* Nonzero means warn for any global function def | |
395 | without separate previous decl. */ | |
396 | ||
397 | int warn_missing_declarations; | |
398 | ||
399 | /* Nonzero means warn about declarations of objects not at | |
400 | file-scope level and about *all* declarations of functions (whether | |
401 | or static) not at file-scope level. Note that we exclude | |
402 | implicit function declarations. To get warnings about those, use | |
403 | -Wimplicit. */ | |
404 | ||
405 | int warn_nested_externs; | |
406 | ||
407 | /* Warn if main is suspicious. */ | |
408 | ||
409 | int warn_main; | |
410 | ||
481c6ce6 | 411 | /* Nonzero means warn about possible violations of sequence point rules. */ |
412 | ||
413 | int warn_sequence_point; | |
414 | ||
bd08a4e6 | 415 | /* Nonzero means to warn about compile-time division by zero. */ |
416 | int warn_div_by_zero = 1; | |
417 | ||
574a6990 | 418 | /* Nonzero means warn about use of implicit int. */ |
419 | ||
420 | int warn_implicit_int; | |
421 | ||
dbf6c367 | 422 | /* Warn about NULL being passed to argument slots marked as requiring |
1cae46be | 423 | non-NULL. */ |
424 | ||
dbf6c367 | 425 | int warn_nonnull; |
426 | ||
574a6990 | 427 | |
428 | /* ObjC language option variables. */ | |
429 | ||
430 | ||
431 | /* Open and close the file for outputting class declarations, if | |
432 | requested (ObjC). */ | |
433 | ||
434 | int flag_gen_declaration; | |
435 | ||
436 | /* Generate code for GNU or NeXT runtime environment. */ | |
437 | ||
438 | #ifdef NEXT_OBJC_RUNTIME | |
439 | int flag_next_runtime = 1; | |
440 | #else | |
441 | int flag_next_runtime = 0; | |
442 | #endif | |
443 | ||
444 | /* Tells the compiler that this is a special run. Do not perform any | |
445 | compiling, instead we are to test some platform dependent features | |
446 | and output a C header file with appropriate definitions. */ | |
447 | ||
448 | int print_struct_values; | |
449 | ||
450 | /* ???. Undocumented. */ | |
451 | ||
452 | const char *constant_string_class_name; | |
453 | ||
454 | /* Warn if multiple methods are seen for the same selector, but with | |
a70e45e3 | 455 | different argument types. Performs the check on the whole selector |
456 | table at the end of compilation. */ | |
574a6990 | 457 | |
458 | int warn_selector; | |
459 | ||
a70e45e3 | 460 | /* Warn if a @selector() is found, and no method with that selector |
461 | has been previously declared. The check is done on each | |
462 | @selector() as soon as it is found - so it warns about forward | |
463 | declarations. */ | |
464 | ||
465 | int warn_undeclared_selector; | |
466 | ||
1cae46be | 467 | /* Warn if methods required by a protocol are not implemented in the |
574a6990 | 468 | class adopting it. When turned off, methods inherited to that |
469 | class are also considered implemented. */ | |
470 | ||
471 | int warn_protocol = 1; | |
472 | ||
473 | ||
474 | /* C++ language option variables. */ | |
475 | ||
476 | ||
477 | /* Nonzero means don't recognize any extension keywords. */ | |
478 | ||
479 | int flag_no_gnu_keywords; | |
480 | ||
481 | /* Nonzero means do emit exported implementations of functions even if | |
482 | they can be inlined. */ | |
483 | ||
484 | int flag_implement_inlines = 1; | |
485 | ||
486 | /* Nonzero means do emit exported implementations of templates, instead of | |
487 | multiple static copies in each file that needs a definition. */ | |
488 | ||
489 | int flag_external_templates; | |
490 | ||
491 | /* Nonzero means that the decision to emit or not emit the implementation of a | |
492 | template depends on where the template is instantiated, rather than where | |
493 | it is defined. */ | |
494 | ||
495 | int flag_alt_external_templates; | |
496 | ||
497 | /* Nonzero means that implicit instantiations will be emitted if needed. */ | |
498 | ||
499 | int flag_implicit_templates = 1; | |
500 | ||
501 | /* Nonzero means that implicit instantiations of inline templates will be | |
502 | emitted if needed, even if instantiations of non-inline templates | |
503 | aren't. */ | |
504 | ||
505 | int flag_implicit_inline_templates = 1; | |
506 | ||
507 | /* Nonzero means generate separate instantiation control files and | |
508 | juggle them at link time. */ | |
509 | ||
510 | int flag_use_repository; | |
511 | ||
512 | /* Nonzero if we want to issue diagnostics that the standard says are not | |
513 | required. */ | |
514 | ||
515 | int flag_optional_diags = 1; | |
516 | ||
517 | /* Nonzero means we should attempt to elide constructors when possible. */ | |
518 | ||
519 | int flag_elide_constructors = 1; | |
520 | ||
521 | /* Nonzero means that member functions defined in class scope are | |
522 | inline by default. */ | |
523 | ||
524 | int flag_default_inline = 1; | |
525 | ||
526 | /* Controls whether compiler generates 'type descriptor' that give | |
527 | run-time type information. */ | |
528 | ||
529 | int flag_rtti = 1; | |
530 | ||
531 | /* Nonzero if we want to conserve space in the .o files. We do this | |
532 | by putting uninitialized data and runtime initialized data into | |
533 | .common instead of .data at the expense of not flagging multiple | |
534 | definitions. */ | |
535 | ||
536 | int flag_conserve_space; | |
537 | ||
538 | /* Nonzero if we want to obey access control semantics. */ | |
539 | ||
540 | int flag_access_control = 1; | |
541 | ||
542 | /* Nonzero if we want to check the return value of new and avoid calling | |
543 | constructors if it is a null pointer. */ | |
544 | ||
545 | int flag_check_new; | |
546 | ||
547 | /* Nonzero if we want the new ISO rules for pushing a new scope for `for' | |
548 | initialization variables. | |
549 | 0: Old rules, set by -fno-for-scope. | |
550 | 2: New ISO rules, set by -ffor-scope. | |
551 | 1: Try to implement new ISO rules, but with backup compatibility | |
552 | (and warnings). This is the default, for now. */ | |
553 | ||
554 | int flag_new_for_scope = 1; | |
555 | ||
556 | /* Nonzero if we want to emit defined symbols with common-like linkage as | |
557 | weak symbols where possible, in order to conform to C++ semantics. | |
558 | Otherwise, emit them as local symbols. */ | |
559 | ||
560 | int flag_weak = 1; | |
561 | ||
562 | /* Nonzero to use __cxa_atexit, rather than atexit, to register | |
563 | destructors for local statics and global objects. */ | |
564 | ||
565 | int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT; | |
566 | ||
574a6990 | 567 | /* Nonzero means make the default pedwarns warnings instead of errors. |
568 | The value of this flag is ignored if -pedantic is specified. */ | |
569 | ||
570 | int flag_permissive; | |
571 | ||
572 | /* Nonzero means to implement standard semantics for exception | |
573 | specifications, calling unexpected if an exception is thrown that | |
574 | doesn't match the specification. Zero means to treat them as | |
575 | assertions and optimize accordingly, but not check them. */ | |
576 | ||
577 | int flag_enforce_eh_specs = 1; | |
578 | ||
221c4c52 | 579 | /* The version of the C++ ABI in use. The following values are |
580 | allowed: | |
581 | ||
1cae46be | 582 | 0: The version of the ABI believed most conformant with the |
221c4c52 | 583 | C++ ABI specification. This ABI may change as bugs are |
584 | discovered and fixed. Therefore, 0 will not necessarily | |
585 | indicate the same ABI in different versions of G++. | |
586 | ||
587 | 1: The version of the ABI first used in G++ 3.2. | |
588 | ||
589 | Additional positive integers will be assigned as new versions of | |
590 | the ABI become the default version of the ABI. */ | |
591 | ||
592 | int flag_abi_version = 1; | |
593 | ||
84fb34c4 | 594 | /* Nonzero means warn about things that will change when compiling |
595 | with an ABI-compliant compiler. */ | |
596 | ||
597 | int warn_abi = 0; | |
598 | ||
6473f3f4 | 599 | /* Nonzero means warn about invalid uses of offsetof. */ |
1cae46be | 600 | |
a861d8ca | 601 | int warn_invalid_offsetof = 1; |
602 | ||
574a6990 | 603 | /* Nonzero means warn about implicit declarations. */ |
604 | ||
605 | int warn_implicit = 1; | |
606 | ||
607 | /* Nonzero means warn when all ctors or dtors are private, and the class | |
608 | has no friends. */ | |
609 | ||
c54151fc | 610 | int warn_ctor_dtor_privacy = 0; |
574a6990 | 611 | |
d10cfa8d | 612 | /* Nonzero means warn in function declared in derived class has the |
574a6990 | 613 | same name as a virtual in the base class, but fails to match the |
614 | type signature of any virtual function in the base class. */ | |
615 | ||
616 | int warn_overloaded_virtual; | |
617 | ||
d10cfa8d | 618 | /* Nonzero means warn when declaring a class that has a non virtual |
574a6990 | 619 | destructor, when it really ought to have a virtual one. */ |
620 | ||
621 | int warn_nonvdtor; | |
622 | ||
d10cfa8d | 623 | /* Nonzero means warn when the compiler will reorder code. */ |
574a6990 | 624 | |
625 | int warn_reorder; | |
626 | ||
d10cfa8d | 627 | /* Nonzero means warn when synthesis behavior differs from Cfront's. */ |
574a6990 | 628 | |
629 | int warn_synth; | |
630 | ||
d10cfa8d | 631 | /* Nonzero means warn when we convert a pointer to member function |
574a6990 | 632 | into a pointer to (void or function). */ |
633 | ||
634 | int warn_pmf2ptr = 1; | |
635 | ||
636 | /* Nonzero means warn about violation of some Effective C++ style rules. */ | |
637 | ||
638 | int warn_ecpp; | |
639 | ||
640 | /* Nonzero means warn where overload resolution chooses a promotion from | |
641 | unsigned to signed over a conversion to an unsigned of the same size. */ | |
642 | ||
643 | int warn_sign_promo; | |
644 | ||
645 | /* Nonzero means warn when an old-style cast is used. */ | |
646 | ||
647 | int warn_old_style_cast; | |
648 | ||
649 | /* Nonzero means warn when non-templatized friend functions are | |
650 | declared within a template */ | |
651 | ||
652 | int warn_nontemplate_friend = 1; | |
653 | ||
654 | /* Nonzero means complain about deprecated features. */ | |
655 | ||
656 | int warn_deprecated = 1; | |
657 | ||
658 | /* Maximum template instantiation depth. This limit is rather | |
659 | arbitrary, but it exists to limit the time it takes to notice | |
660 | infinite template instantiations. */ | |
661 | ||
662 | int max_tinst_depth = 500; | |
663 | ||
664 | ||
665 | ||
988fc1d1 | 666 | /* The elements of `ridpointers' are identifier nodes for the reserved |
667 | type names and storage classes. It is indexed by a RID_... value. */ | |
668 | tree *ridpointers; | |
669 | ||
1cae46be | 670 | tree (*make_fname_decl) (tree, int); |
9e5a737d | 671 | |
e41f0d80 | 672 | /* If non-NULL, the address of a language-specific function that takes |
673 | any action required right before expand_function_end is called. */ | |
1cae46be | 674 | void (*lang_expand_function_end) (void); |
e41f0d80 | 675 | |
e78703c1 | 676 | /* Nonzero means the expression being parsed will never be evaluated. |
677 | This is a count, since unevaluated expressions can nest. */ | |
678 | int skip_evaluation; | |
679 | ||
2c0e001b | 680 | /* Information about how a function name is generated. */ |
65b7f83f | 681 | struct fname_var_t |
682 | { | |
e99c3a1d | 683 | tree *const decl; /* pointer to the VAR_DECL. */ |
684 | const unsigned rid; /* RID number for the identifier. */ | |
685 | const int pretty; /* How pretty is it? */ | |
65b7f83f | 686 | }; |
687 | ||
2c0e001b | 688 | /* The three ways of getting then name of the current function. */ |
65b7f83f | 689 | |
690 | const struct fname_var_t fname_vars[] = | |
691 | { | |
2c0e001b | 692 | /* C99 compliant __func__, must be first. */ |
65b7f83f | 693 | {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0}, |
2c0e001b | 694 | /* GCC __FUNCTION__ compliant. */ |
65b7f83f | 695 | {&function_name_decl_node, RID_FUNCTION_NAME, 0}, |
2c0e001b | 696 | /* GCC __PRETTY_FUNCTION__ compliant. */ |
65b7f83f | 697 | {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1}, |
698 | {NULL, 0, 0}, | |
699 | }; | |
700 | ||
1cae46be | 701 | static int constant_fits_type_p (tree, tree); |
be43ff5a | 702 | |
9fc84d48 | 703 | /* Keep a stack of if statements. We record the number of compound |
704 | statements seen up to the if keyword, as well as the line number | |
705 | and file of the if. If a potentially ambiguous else is seen, that | |
706 | fact is recorded; the warning is issued when we can be sure that | |
707 | the enclosing if statement does not have an else branch. */ | |
708 | typedef struct | |
709 | { | |
710 | int compstmt_count; | |
834fc4a0 | 711 | location_t locus; |
9fc84d48 | 712 | int needs_warning; |
e41f0d80 | 713 | tree if_stmt; |
9fc84d48 | 714 | } if_elt; |
715 | ||
716 | static if_elt *if_stack; | |
31f820d2 | 717 | |
718 | /* Amount of space in the if statement stack. */ | |
719 | static int if_stack_space = 0; | |
720 | ||
721 | /* Stack pointer. */ | |
722 | static int if_stack_pointer = 0; | |
723 | ||
1cae46be | 724 | static tree handle_packed_attribute (tree *, tree, tree, int, bool *); |
725 | static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *); | |
726 | static tree handle_common_attribute (tree *, tree, tree, int, bool *); | |
727 | static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *); | |
728 | static tree handle_noinline_attribute (tree *, tree, tree, int, bool *); | |
729 | static tree handle_always_inline_attribute (tree *, tree, tree, int, | |
730 | bool *); | |
731 | static tree handle_used_attribute (tree *, tree, tree, int, bool *); | |
732 | static tree handle_unused_attribute (tree *, tree, tree, int, bool *); | |
733 | static tree handle_const_attribute (tree *, tree, tree, int, bool *); | |
734 | static tree handle_transparent_union_attribute (tree *, tree, tree, | |
735 | int, bool *); | |
736 | static tree handle_constructor_attribute (tree *, tree, tree, int, bool *); | |
737 | static tree handle_destructor_attribute (tree *, tree, tree, int, bool *); | |
738 | static tree handle_mode_attribute (tree *, tree, tree, int, bool *); | |
739 | static tree handle_section_attribute (tree *, tree, tree, int, bool *); | |
740 | static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); | |
741 | static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; | |
742 | static tree handle_alias_attribute (tree *, tree, tree, int, bool *); | |
743 | static tree handle_visibility_attribute (tree *, tree, tree, int, | |
744 | bool *); | |
745 | static tree handle_tls_model_attribute (tree *, tree, tree, int, | |
746 | bool *); | |
747 | static tree handle_no_instrument_function_attribute (tree *, tree, | |
748 | tree, int, bool *); | |
749 | static tree handle_malloc_attribute (tree *, tree, tree, int, bool *); | |
750 | static tree handle_no_limit_stack_attribute (tree *, tree, tree, int, | |
751 | bool *); | |
752 | static tree handle_pure_attribute (tree *, tree, tree, int, bool *); | |
753 | static tree handle_deprecated_attribute (tree *, tree, tree, int, | |
754 | bool *); | |
755 | static tree handle_vector_size_attribute (tree *, tree, tree, int, | |
756 | bool *); | |
757 | static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *); | |
758 | static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *); | |
759 | static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *); | |
760 | static tree vector_size_helper (tree, tree); | |
761 | ||
762 | static void check_function_nonnull (tree, tree); | |
763 | static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); | |
764 | static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT); | |
765 | static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); | |
860251be | 766 | static int resort_field_decl_cmp (const void *, const void *); |
dbf6c367 | 767 | |
f8e93a2e | 768 | /* Table of machine-independent attributes common to all C-like languages. */ |
769 | const struct attribute_spec c_common_attribute_table[] = | |
770 | { | |
771 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */ | |
772 | { "packed", 0, 0, false, false, false, | |
1cae46be | 773 | handle_packed_attribute }, |
f8e93a2e | 774 | { "nocommon", 0, 0, true, false, false, |
775 | handle_nocommon_attribute }, | |
776 | { "common", 0, 0, true, false, false, | |
777 | handle_common_attribute }, | |
778 | /* FIXME: logically, noreturn attributes should be listed as | |
779 | "false, true, true" and apply to function types. But implementing this | |
780 | would require all the places in the compiler that use TREE_THIS_VOLATILE | |
781 | on a decl to identify non-returning functions to be located and fixed | |
782 | to check the function type instead. */ | |
783 | { "noreturn", 0, 0, true, false, false, | |
784 | handle_noreturn_attribute }, | |
785 | { "volatile", 0, 0, true, false, false, | |
786 | handle_noreturn_attribute }, | |
787 | { "noinline", 0, 0, true, false, false, | |
788 | handle_noinline_attribute }, | |
789 | { "always_inline", 0, 0, true, false, false, | |
790 | handle_always_inline_attribute }, | |
791 | { "used", 0, 0, true, false, false, | |
792 | handle_used_attribute }, | |
793 | { "unused", 0, 0, false, false, false, | |
794 | handle_unused_attribute }, | |
795 | /* The same comments as for noreturn attributes apply to const ones. */ | |
796 | { "const", 0, 0, true, false, false, | |
797 | handle_const_attribute }, | |
798 | { "transparent_union", 0, 0, false, false, false, | |
799 | handle_transparent_union_attribute }, | |
800 | { "constructor", 0, 0, true, false, false, | |
801 | handle_constructor_attribute }, | |
802 | { "destructor", 0, 0, true, false, false, | |
803 | handle_destructor_attribute }, | |
804 | { "mode", 1, 1, false, true, false, | |
805 | handle_mode_attribute }, | |
806 | { "section", 1, 1, true, false, false, | |
807 | handle_section_attribute }, | |
808 | { "aligned", 0, 1, false, false, false, | |
809 | handle_aligned_attribute }, | |
810 | { "weak", 0, 0, true, false, false, | |
811 | handle_weak_attribute }, | |
812 | { "alias", 1, 1, true, false, false, | |
813 | handle_alias_attribute }, | |
814 | { "no_instrument_function", 0, 0, true, false, false, | |
815 | handle_no_instrument_function_attribute }, | |
816 | { "malloc", 0, 0, true, false, false, | |
817 | handle_malloc_attribute }, | |
818 | { "no_stack_limit", 0, 0, true, false, false, | |
819 | handle_no_limit_stack_attribute }, | |
820 | { "pure", 0, 0, true, false, false, | |
821 | handle_pure_attribute }, | |
822 | { "deprecated", 0, 0, false, false, false, | |
823 | handle_deprecated_attribute }, | |
824 | { "vector_size", 1, 1, false, true, false, | |
825 | handle_vector_size_attribute }, | |
826 | { "visibility", 1, 1, true, false, false, | |
827 | handle_visibility_attribute }, | |
24dfead4 | 828 | { "tls_model", 1, 1, true, false, false, |
829 | handle_tls_model_attribute }, | |
dbf6c367 | 830 | { "nonnull", 0, -1, false, true, true, |
831 | handle_nonnull_attribute }, | |
fa987697 | 832 | { "nothrow", 0, 0, true, false, false, |
833 | handle_nothrow_attribute }, | |
cb59f969 | 834 | { "may_alias", 0, 0, false, true, false, NULL }, |
7acb29a3 | 835 | { "cleanup", 1, 1, true, false, false, |
836 | handle_cleanup_attribute }, | |
f8e93a2e | 837 | { NULL, 0, 0, false, false, false, NULL } |
838 | }; | |
839 | ||
840 | /* Give the specifications for the format attributes, used by C and all | |
d716ce75 | 841 | descendants. */ |
f8e93a2e | 842 | |
843 | const struct attribute_spec c_common_format_attribute_table[] = | |
844 | { | |
845 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */ | |
846 | { "format", 3, 3, false, true, true, | |
847 | handle_format_attribute }, | |
848 | { "format_arg", 1, 1, false, true, true, | |
849 | handle_format_arg_attribute }, | |
850 | { NULL, 0, 0, false, false, false, NULL } | |
851 | }; | |
852 | ||
e41f0d80 | 853 | /* Record the start of an if-then, and record the start of it |
e87da6ae | 854 | for ambiguous else detection. |
855 | ||
856 | COND is the condition for the if-then statement. | |
857 | ||
858 | IF_STMT is the statement node that has already been created for | |
859 | this if-then statement. It is created before parsing the | |
860 | condition to keep line number information accurate. */ | |
9fc84d48 | 861 | |
31f820d2 | 862 | void |
1cae46be | 863 | c_expand_start_cond (tree cond, int compstmt_count, tree if_stmt) |
31f820d2 | 864 | { |
865 | /* Make sure there is enough space on the stack. */ | |
866 | if (if_stack_space == 0) | |
867 | { | |
868 | if_stack_space = 10; | |
f0af5a88 | 869 | if_stack = xmalloc (10 * sizeof (if_elt)); |
31f820d2 | 870 | } |
871 | else if (if_stack_space == if_stack_pointer) | |
872 | { | |
873 | if_stack_space += 10; | |
f0af5a88 | 874 | if_stack = xrealloc (if_stack, if_stack_space * sizeof (if_elt)); |
31f820d2 | 875 | } |
9fc84d48 | 876 | |
e41f0d80 | 877 | IF_COND (if_stmt) = cond; |
878 | add_stmt (if_stmt); | |
879 | ||
31f820d2 | 880 | /* Record this if statement. */ |
9fc84d48 | 881 | if_stack[if_stack_pointer].compstmt_count = compstmt_count; |
bca5a169 | 882 | if_stack[if_stack_pointer].locus = input_location; |
9fc84d48 | 883 | if_stack[if_stack_pointer].needs_warning = 0; |
e41f0d80 | 884 | if_stack[if_stack_pointer].if_stmt = if_stmt; |
9fc84d48 | 885 | if_stack_pointer++; |
e41f0d80 | 886 | } |
31f820d2 | 887 | |
e41f0d80 | 888 | /* Called after the then-clause for an if-statement is processed. */ |
889 | ||
890 | void | |
1cae46be | 891 | c_finish_then (void) |
e41f0d80 | 892 | { |
893 | tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt; | |
894 | RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt)); | |
31f820d2 | 895 | } |
896 | ||
e41f0d80 | 897 | /* Record the end of an if-then. Optionally warn if a nested |
9fc84d48 | 898 | if statement had an ambiguous else clause. */ |
899 | ||
31f820d2 | 900 | void |
1cae46be | 901 | c_expand_end_cond (void) |
31f820d2 | 902 | { |
903 | if_stack_pointer--; | |
9fc84d48 | 904 | if (if_stack[if_stack_pointer].needs_warning) |
834fc4a0 | 905 | warning ("%Hsuggest explicit braces to avoid ambiguous `else'", |
906 | &if_stack[if_stack_pointer].locus); | |
e41f0d80 | 907 | last_expr_type = NULL_TREE; |
31f820d2 | 908 | } |
909 | ||
e41f0d80 | 910 | /* Called between the then-clause and the else-clause |
9fc84d48 | 911 | of an if-then-else. */ |
912 | ||
31f820d2 | 913 | void |
1cae46be | 914 | c_expand_start_else (void) |
31f820d2 | 915 | { |
9fc84d48 | 916 | /* An ambiguous else warning must be generated for the enclosing if |
917 | statement, unless we see an else branch for that one, too. */ | |
31f820d2 | 918 | if (warn_parentheses |
919 | && if_stack_pointer > 1 | |
9fc84d48 | 920 | && (if_stack[if_stack_pointer - 1].compstmt_count |
921 | == if_stack[if_stack_pointer - 2].compstmt_count)) | |
922 | if_stack[if_stack_pointer - 2].needs_warning = 1; | |
923 | ||
924 | /* Even if a nested if statement had an else branch, it can't be | |
925 | ambiguous if this one also has an else. So don't warn in that | |
926 | case. Also don't warn for any if statements nested in this else. */ | |
927 | if_stack[if_stack_pointer - 1].needs_warning = 0; | |
928 | if_stack[if_stack_pointer - 1].compstmt_count--; | |
e41f0d80 | 929 | } |
930 | ||
931 | /* Called after the else-clause for an if-statement is processed. */ | |
31f820d2 | 932 | |
e41f0d80 | 933 | void |
1cae46be | 934 | c_finish_else (void) |
e41f0d80 | 935 | { |
936 | tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt; | |
937 | RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt)); | |
31f820d2 | 938 | } |
939 | ||
e87da6ae | 940 | /* Begin an if-statement. Returns a newly created IF_STMT if |
941 | appropriate. | |
942 | ||
943 | Unlike the C++ front-end, we do not call add_stmt here; it is | |
944 | probably safe to do so, but I am not very familiar with this | |
945 | code so I am being extra careful not to change its behavior | |
946 | beyond what is strictly necessary for correctness. */ | |
947 | ||
948 | tree | |
1cae46be | 949 | c_begin_if_stmt (void) |
e87da6ae | 950 | { |
951 | tree r; | |
952 | r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE); | |
953 | return r; | |
954 | } | |
955 | ||
956 | /* Begin a while statement. Returns a newly created WHILE_STMT if | |
957 | appropriate. | |
958 | ||
959 | Unlike the C++ front-end, we do not call add_stmt here; it is | |
960 | probably safe to do so, but I am not very familiar with this | |
961 | code so I am being extra careful not to change its behavior | |
962 | beyond what is strictly necessary for correctness. */ | |
963 | ||
964 | tree | |
1cae46be | 965 | c_begin_while_stmt (void) |
e87da6ae | 966 | { |
967 | tree r; | |
968 | r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE); | |
969 | return r; | |
970 | } | |
971 | ||
972 | void | |
1cae46be | 973 | c_finish_while_stmt_cond (tree cond, tree while_stmt) |
e87da6ae | 974 | { |
975 | WHILE_COND (while_stmt) = cond; | |
976 | } | |
977 | ||
2c0e001b | 978 | /* Push current bindings for the function name VAR_DECLS. */ |
f4e3c278 | 979 | |
980 | void | |
1cae46be | 981 | start_fname_decls (void) |
f4e3c278 | 982 | { |
65b7f83f | 983 | unsigned ix; |
984 | tree saved = NULL_TREE; | |
1cae46be | 985 | |
65b7f83f | 986 | for (ix = 0; fname_vars[ix].decl; ix++) |
987 | { | |
988 | tree decl = *fname_vars[ix].decl; | |
f4e3c278 | 989 | |
65b7f83f | 990 | if (decl) |
991 | { | |
992 | saved = tree_cons (decl, build_int_2 (ix, 0), saved); | |
993 | *fname_vars[ix].decl = NULL_TREE; | |
994 | } | |
995 | } | |
996 | if (saved || saved_function_name_decls) | |
997 | /* Normally they'll have been NULL, so only push if we've got a | |
998 | stack, or they are non-NULL. */ | |
999 | saved_function_name_decls = tree_cons (saved, NULL_TREE, | |
1000 | saved_function_name_decls); | |
1001 | } | |
1002 | ||
1003 | /* Finish up the current bindings, adding them into the | |
1004 | current function's statement tree. This is done by wrapping the | |
1005 | function's body in a COMPOUND_STMT containing these decls too. This | |
1006 | must be done _before_ finish_stmt_tree is called. If there is no | |
1007 | current function, we must be at file scope and no statements are | |
2c0e001b | 1008 | involved. Pop the previous bindings. */ |
65b7f83f | 1009 | |
1010 | void | |
1cae46be | 1011 | finish_fname_decls (void) |
65b7f83f | 1012 | { |
1013 | unsigned ix; | |
1014 | tree body = NULL_TREE; | |
1015 | tree stack = saved_function_name_decls; | |
1016 | ||
1017 | for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack)) | |
1018 | body = chainon (TREE_VALUE (stack), body); | |
1cae46be | 1019 | |
65b7f83f | 1020 | if (body) |
1021 | { | |
81010c97 | 1022 | /* They were called into existence, so add to statement tree. Add |
1023 | the DECL_STMTs inside the outermost scope. */ | |
1024 | tree *p = &DECL_SAVED_TREE (current_function_decl); | |
1025 | /* Skip the dummy EXPR_STMT and any EH_SPEC_BLOCK. */ | |
1026 | while (TREE_CODE (*p) != COMPOUND_STMT) | |
1027 | p = &TREE_CHAIN (*p); | |
1028 | p = &COMPOUND_BODY (*p); | |
1029 | if (TREE_CODE (*p) == SCOPE_STMT) | |
1030 | p = &TREE_CHAIN (*p); | |
1031 | ||
1032 | body = chainon (body, *p); | |
1033 | *p = body; | |
65b7f83f | 1034 | } |
1cae46be | 1035 | |
65b7f83f | 1036 | for (ix = 0; fname_vars[ix].decl; ix++) |
1037 | *fname_vars[ix].decl = NULL_TREE; | |
1cae46be | 1038 | |
65b7f83f | 1039 | if (stack) |
f4e3c278 | 1040 | { |
2c0e001b | 1041 | /* We had saved values, restore them. */ |
65b7f83f | 1042 | tree saved; |
1043 | ||
1044 | for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved)) | |
1045 | { | |
1046 | tree decl = TREE_PURPOSE (saved); | |
1047 | unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved)); | |
1cae46be | 1048 | |
65b7f83f | 1049 | *fname_vars[ix].decl = decl; |
1050 | } | |
1051 | stack = TREE_CHAIN (stack); | |
f4e3c278 | 1052 | } |
65b7f83f | 1053 | saved_function_name_decls = stack; |
1054 | } | |
1055 | ||
81010c97 | 1056 | /* Return the text name of the current function, suitably prettified |
2c0e001b | 1057 | by PRETTY_P. */ |
65b7f83f | 1058 | |
1059 | const char * | |
1cae46be | 1060 | fname_as_string (int pretty_p) |
65b7f83f | 1061 | { |
1062 | const char *name = NULL; | |
1cae46be | 1063 | |
65b7f83f | 1064 | if (pretty_p) |
1065 | name = (current_function_decl | |
96554925 | 1066 | ? (*lang_hooks.decl_printable_name) (current_function_decl, 2) |
65b7f83f | 1067 | : "top level"); |
1068 | else if (current_function_decl && DECL_NAME (current_function_decl)) | |
1069 | name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl)); | |
f4e3c278 | 1070 | else |
65b7f83f | 1071 | name = ""; |
1072 | return name; | |
1073 | } | |
1074 | ||
65b7f83f | 1075 | /* Return the VAR_DECL for a const char array naming the current |
1076 | function. If the VAR_DECL has not yet been created, create it | |
1077 | now. RID indicates how it should be formatted and IDENTIFIER_NODE | |
1078 | ID is its name (unfortunately C and C++ hold the RID values of | |
1079 | keywords in different places, so we can't derive RID from ID in | |
dd5b4b36 | 1080 | this language independent code. */ |
65b7f83f | 1081 | |
1082 | tree | |
1cae46be | 1083 | fname_decl (unsigned int rid, tree id) |
65b7f83f | 1084 | { |
1085 | unsigned ix; | |
1086 | tree decl = NULL_TREE; | |
1087 | ||
1088 | for (ix = 0; fname_vars[ix].decl; ix++) | |
1089 | if (fname_vars[ix].rid == rid) | |
1090 | break; | |
1091 | ||
1092 | decl = *fname_vars[ix].decl; | |
1093 | if (!decl) | |
f4e3c278 | 1094 | { |
65b7f83f | 1095 | tree saved_last_tree = last_tree; |
2222b3c6 | 1096 | /* If a tree is built here, it would normally have the lineno of |
1097 | the current statement. Later this tree will be moved to the | |
1098 | beginning of the function and this line number will be wrong. | |
1099 | To avoid this problem set the lineno to 0 here; that prevents | |
7299020b | 1100 | it from appearing in the RTL. */ |
fa70df70 | 1101 | int saved_lineno = input_line; |
1102 | input_line = 0; | |
1cae46be | 1103 | |
65b7f83f | 1104 | decl = (*make_fname_decl) (id, fname_vars[ix].pretty); |
1105 | if (last_tree != saved_last_tree) | |
1106 | { | |
1107 | /* We created some statement tree for the decl. This belongs | |
1108 | at the start of the function, so remove it now and reinsert | |
2c0e001b | 1109 | it after the function is complete. */ |
65b7f83f | 1110 | tree stmts = TREE_CHAIN (saved_last_tree); |
1111 | ||
1112 | TREE_CHAIN (saved_last_tree) = NULL_TREE; | |
1113 | last_tree = saved_last_tree; | |
1114 | saved_function_name_decls = tree_cons (decl, stmts, | |
1115 | saved_function_name_decls); | |
1116 | } | |
1117 | *fname_vars[ix].decl = decl; | |
fa70df70 | 1118 | input_line = saved_lineno; |
f4e3c278 | 1119 | } |
65b7f83f | 1120 | if (!ix && !current_function_decl) |
f2791601 | 1121 | pedwarn ("%H'%D' is not defined outside of function scope", |
1122 | &DECL_SOURCE_LOCATION (decl), decl); | |
81010c97 | 1123 | |
65b7f83f | 1124 | return decl; |
f4e3c278 | 1125 | } |
1126 | ||
070236f0 | 1127 | /* Given a STRING_CST, give it a suitable array-of-chars data type. */ |
b0fc3e72 | 1128 | |
1129 | tree | |
1cae46be | 1130 | fix_string_type (tree value) |
b0fc3e72 | 1131 | { |
070236f0 | 1132 | const int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT; |
1133 | const int wide_flag = TREE_TYPE (value) == wchar_array_type_node; | |
82cfc7f7 | 1134 | const int nchars_max = flag_isoc99 ? 4095 : 509; |
070236f0 | 1135 | int length = TREE_STRING_LENGTH (value); |
1136 | int nchars; | |
b0fc3e72 | 1137 | |
73be5127 | 1138 | /* Compute the number of elements, for the array type. */ |
b0fc3e72 | 1139 | nchars = wide_flag ? length / wchar_bytes : length; |
1140 | ||
c0f19401 | 1141 | if (pedantic && nchars - 1 > nchars_max && !c_dialect_cxx ()) |
960bf856 | 1142 | pedwarn ("string length `%d' is greater than the length `%d' ISO C%d compilers are required to support", |
89e32525 | 1143 | nchars - 1, nchars_max, flag_isoc99 ? 99 : 89); |
82cfc7f7 | 1144 | |
b0fc3e72 | 1145 | /* Create the array type for the string constant. |
1146 | -Wwrite-strings says make the string constant an array of const char | |
3a10ba35 | 1147 | so that copying it to a non-const pointer will get a warning. |
1148 | For C++, this is the standard behavior. */ | |
455730ef | 1149 | if (flag_const_strings && ! flag_writable_strings) |
b0fc3e72 | 1150 | { |
1151 | tree elements | |
1152 | = build_type_variant (wide_flag ? wchar_type_node : char_type_node, | |
1153 | 1, 0); | |
1154 | TREE_TYPE (value) | |
1155 | = build_array_type (elements, | |
1156 | build_index_type (build_int_2 (nchars - 1, 0))); | |
1157 | } | |
1158 | else | |
1159 | TREE_TYPE (value) | |
1160 | = build_array_type (wide_flag ? wchar_type_node : char_type_node, | |
1161 | build_index_type (build_int_2 (nchars - 1, 0))); | |
3a10ba35 | 1162 | |
b8e3b7ad | 1163 | TREE_CONSTANT (value) = 1; |
1164 | TREE_READONLY (value) = ! flag_writable_strings; | |
b0fc3e72 | 1165 | TREE_STATIC (value) = 1; |
1166 | return value; | |
1167 | } | |
1168 | \f | |
2a1736ed | 1169 | /* Print a warning if a constant expression had overflow in folding. |
1170 | Invoke this function on every expression that the language | |
1171 | requires to be a constant expression. | |
1172 | Note the ANSI C standard says it is erroneous for a | |
1173 | constant expression to overflow. */ | |
b2806639 | 1174 | |
1175 | void | |
1cae46be | 1176 | constant_expression_warning (tree value) |
b2806639 | 1177 | { |
837e1122 | 1178 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
886cfd4f | 1179 | || TREE_CODE (value) == VECTOR_CST |
837e1122 | 1180 | || TREE_CODE (value) == COMPLEX_CST) |
1181 | && TREE_CONSTANT_OVERFLOW (value) && pedantic) | |
1182 | pedwarn ("overflow in constant expression"); | |
2a1736ed | 1183 | } |
1184 | ||
1185 | /* Print a warning if an expression had overflow in folding. | |
1186 | Invoke this function on every expression that | |
1187 | (1) appears in the source code, and | |
1188 | (2) might be a constant expression that overflowed, and | |
1189 | (3) is not already checked by convert_and_check; | |
1190 | however, do not invoke this function on operands of explicit casts. */ | |
1191 | ||
1192 | void | |
1cae46be | 1193 | overflow_warning (tree value) |
2a1736ed | 1194 | { |
837e1122 | 1195 | if ((TREE_CODE (value) == INTEGER_CST |
1196 | || (TREE_CODE (value) == COMPLEX_CST | |
1197 | && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)) | |
1198 | && TREE_OVERFLOW (value)) | |
2a1736ed | 1199 | { |
b04da9b5 | 1200 | TREE_OVERFLOW (value) = 0; |
e78703c1 | 1201 | if (skip_evaluation == 0) |
1202 | warning ("integer overflow in expression"); | |
2a1736ed | 1203 | } |
837e1122 | 1204 | else if ((TREE_CODE (value) == REAL_CST |
1205 | || (TREE_CODE (value) == COMPLEX_CST | |
1206 | && TREE_CODE (TREE_REALPART (value)) == REAL_CST)) | |
1207 | && TREE_OVERFLOW (value)) | |
1208 | { | |
1209 | TREE_OVERFLOW (value) = 0; | |
e78703c1 | 1210 | if (skip_evaluation == 0) |
1211 | warning ("floating point overflow in expression"); | |
837e1122 | 1212 | } |
886cfd4f | 1213 | else if (TREE_CODE (value) == VECTOR_CST && TREE_OVERFLOW (value)) |
1214 | { | |
1215 | TREE_OVERFLOW (value) = 0; | |
1216 | if (skip_evaluation == 0) | |
1217 | warning ("vector overflow in expression"); | |
1218 | } | |
2a1736ed | 1219 | } |
1220 | ||
1221 | /* Print a warning if a large constant is truncated to unsigned, | |
1222 | or if -Wconversion is used and a constant < 0 is converted to unsigned. | |
1223 | Invoke this function on every expression that might be implicitly | |
1224 | converted to an unsigned type. */ | |
1225 | ||
1226 | void | |
1cae46be | 1227 | unsigned_conversion_warning (tree result, tree operand) |
2a1736ed | 1228 | { |
4070745f | 1229 | tree type = TREE_TYPE (result); |
1230 | ||
2a1736ed | 1231 | if (TREE_CODE (operand) == INTEGER_CST |
4070745f | 1232 | && TREE_CODE (type) == INTEGER_TYPE |
1233 | && TREE_UNSIGNED (type) | |
e78703c1 | 1234 | && skip_evaluation == 0 |
4070745f | 1235 | && !int_fits_type_p (operand, type)) |
2a1736ed | 1236 | { |
4070745f | 1237 | if (!int_fits_type_p (operand, c_common_signed_type (type))) |
2a1736ed | 1238 | /* This detects cases like converting -129 or 256 to unsigned char. */ |
454b5afb | 1239 | warning ("large integer implicitly truncated to unsigned type"); |
2a1736ed | 1240 | else if (warn_conversion) |
454b5afb | 1241 | warning ("negative integer implicitly converted to unsigned type"); |
2a1736ed | 1242 | } |
1243 | } | |
1244 | ||
7f728835 | 1245 | /* Nonzero if constant C has a value that is permissible |
1246 | for type TYPE (an INTEGER_TYPE). */ | |
1247 | ||
1248 | static int | |
1cae46be | 1249 | constant_fits_type_p (tree c, tree type) |
7f728835 | 1250 | { |
1251 | if (TREE_CODE (c) == INTEGER_CST) | |
1252 | return int_fits_type_p (c, type); | |
1253 | ||
1254 | c = convert (type, c); | |
1255 | return !TREE_OVERFLOW (c); | |
1cae46be | 1256 | } |
7f728835 | 1257 | |
2a1736ed | 1258 | /* Convert EXPR to TYPE, warning about conversion problems with constants. |
1259 | Invoke this function on every expression that is converted implicitly, | |
1260 | i.e. because of language rules and not because of an explicit cast. */ | |
1261 | ||
1262 | tree | |
1cae46be | 1263 | convert_and_check (tree type, tree expr) |
2a1736ed | 1264 | { |
1265 | tree t = convert (type, expr); | |
1266 | if (TREE_CODE (t) == INTEGER_CST) | |
1267 | { | |
b04da9b5 | 1268 | if (TREE_OVERFLOW (t)) |
2a1736ed | 1269 | { |
b04da9b5 | 1270 | TREE_OVERFLOW (t) = 0; |
1271 | ||
2c4232f8 | 1272 | /* Do not diagnose overflow in a constant expression merely |
1273 | because a conversion overflowed. */ | |
1274 | TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr); | |
1275 | ||
b04da9b5 | 1276 | /* No warning for converting 0x80000000 to int. */ |
1277 | if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr)) | |
1278 | && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
1279 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr)))) | |
96730c20 | 1280 | /* If EXPR fits in the unsigned version of TYPE, |
1281 | don't warn unless pedantic. */ | |
e78703c1 | 1282 | if ((pedantic |
1283 | || TREE_UNSIGNED (type) | |
4070745f | 1284 | || ! constant_fits_type_p (expr, |
1285 | c_common_unsigned_type (type))) | |
e78703c1 | 1286 | && skip_evaluation == 0) |
d1f11193 | 1287 | warning ("overflow in implicit constant conversion"); |
2a1736ed | 1288 | } |
1289 | else | |
1290 | unsigned_conversion_warning (t, expr); | |
1291 | } | |
1292 | return t; | |
b2806639 | 1293 | } |
1294 | \f | |
4e91a871 | 1295 | /* A node in a list that describes references to variables (EXPR), which are |
1296 | either read accesses if WRITER is zero, or write accesses, in which case | |
1297 | WRITER is the parent of EXPR. */ | |
1298 | struct tlist | |
1299 | { | |
1300 | struct tlist *next; | |
1301 | tree expr, writer; | |
1302 | }; | |
1303 | ||
1304 | /* Used to implement a cache the results of a call to verify_tree. We only | |
1305 | use this for SAVE_EXPRs. */ | |
1306 | struct tlist_cache | |
1307 | { | |
1308 | struct tlist_cache *next; | |
1309 | struct tlist *cache_before_sp; | |
1310 | struct tlist *cache_after_sp; | |
1311 | tree expr; | |
481c6ce6 | 1312 | }; |
1313 | ||
4e91a871 | 1314 | /* Obstack to use when allocating tlist structures, and corresponding |
1315 | firstobj. */ | |
1316 | static struct obstack tlist_obstack; | |
1317 | static char *tlist_firstobj = 0; | |
1318 | ||
1319 | /* Keep track of the identifiers we've warned about, so we can avoid duplicate | |
1320 | warnings. */ | |
1321 | static struct tlist *warned_ids; | |
1322 | /* SAVE_EXPRs need special treatment. We process them only once and then | |
1323 | cache the results. */ | |
1324 | static struct tlist_cache *save_expr_cache; | |
1325 | ||
1cae46be | 1326 | static void add_tlist (struct tlist **, struct tlist *, tree, int); |
1327 | static void merge_tlist (struct tlist **, struct tlist *, int); | |
1328 | static void verify_tree (tree, struct tlist **, struct tlist **, tree); | |
1329 | static int warning_candidate_p (tree); | |
1330 | static void warn_for_collisions (struct tlist *); | |
1331 | static void warn_for_collisions_1 (tree, tree, struct tlist *, int); | |
1332 | static struct tlist *new_tlist (struct tlist *, tree, tree); | |
1333 | static void verify_sequence_points (tree); | |
481c6ce6 | 1334 | |
4e91a871 | 1335 | /* Create a new struct tlist and fill in its fields. */ |
1336 | static struct tlist * | |
1cae46be | 1337 | new_tlist (struct tlist *next, tree t, tree writer) |
4e91a871 | 1338 | { |
1339 | struct tlist *l; | |
f0af5a88 | 1340 | l = obstack_alloc (&tlist_obstack, sizeof *l); |
4e91a871 | 1341 | l->next = next; |
1342 | l->expr = t; | |
1343 | l->writer = writer; | |
1344 | return l; | |
1345 | } | |
1346 | ||
1347 | /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER | |
1348 | is nonnull, we ignore any node we find which has a writer equal to it. */ | |
1349 | ||
1350 | static void | |
1cae46be | 1351 | add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy) |
4e91a871 | 1352 | { |
1353 | while (add) | |
1354 | { | |
1355 | struct tlist *next = add->next; | |
1356 | if (! copy) | |
1357 | add->next = *to; | |
1358 | if (! exclude_writer || add->writer != exclude_writer) | |
1359 | *to = copy ? new_tlist (*to, add->expr, add->writer) : add; | |
1360 | add = next; | |
1361 | } | |
1362 | } | |
1363 | ||
1364 | /* Merge the nodes of ADD into TO. This merging process is done so that for | |
1365 | each variable that already exists in TO, no new node is added; however if | |
1366 | there is a write access recorded in ADD, and an occurrence on TO is only | |
1367 | a read access, then the occurrence in TO will be modified to record the | |
1368 | write. */ | |
481c6ce6 | 1369 | |
1370 | static void | |
1cae46be | 1371 | merge_tlist (struct tlist **to, struct tlist *add, int copy) |
4e91a871 | 1372 | { |
1373 | struct tlist **end = to; | |
1374 | ||
1375 | while (*end) | |
1376 | end = &(*end)->next; | |
1377 | ||
1378 | while (add) | |
1379 | { | |
1380 | int found = 0; | |
1381 | struct tlist *tmp2; | |
1382 | struct tlist *next = add->next; | |
1383 | ||
1384 | for (tmp2 = *to; tmp2; tmp2 = tmp2->next) | |
1385 | if (tmp2->expr == add->expr) | |
1386 | { | |
1387 | found = 1; | |
1388 | if (! tmp2->writer) | |
1389 | tmp2->writer = add->writer; | |
1390 | } | |
1391 | if (! found) | |
1392 | { | |
1393 | *end = copy ? add : new_tlist (NULL, add->expr, add->writer); | |
1394 | end = &(*end)->next; | |
1395 | *end = 0; | |
1396 | } | |
1397 | add = next; | |
1398 | } | |
1399 | } | |
1400 | ||
1401 | /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable | |
1402 | references in list LIST conflict with it, excluding reads if ONLY writers | |
1403 | is nonzero. */ | |
1404 | ||
1405 | static void | |
1cae46be | 1406 | warn_for_collisions_1 (tree written, tree writer, struct tlist *list, |
1407 | int only_writes) | |
4e91a871 | 1408 | { |
1409 | struct tlist *tmp; | |
1410 | ||
1411 | /* Avoid duplicate warnings. */ | |
1412 | for (tmp = warned_ids; tmp; tmp = tmp->next) | |
1413 | if (tmp->expr == written) | |
1414 | return; | |
1415 | ||
1416 | while (list) | |
1417 | { | |
1418 | if (list->expr == written | |
1419 | && list->writer != writer | |
1420 | && (! only_writes || list->writer)) | |
1421 | { | |
1422 | warned_ids = new_tlist (warned_ids, written, NULL_TREE); | |
1423 | warning ("operation on `%s' may be undefined", | |
1424 | IDENTIFIER_POINTER (DECL_NAME (list->expr))); | |
1425 | } | |
1426 | list = list->next; | |
1427 | } | |
1428 | } | |
1429 | ||
1430 | /* Given a list LIST of references to variables, find whether any of these | |
1431 | can cause conflicts due to missing sequence points. */ | |
1432 | ||
1433 | static void | |
1cae46be | 1434 | warn_for_collisions (struct tlist *list) |
4e91a871 | 1435 | { |
1436 | struct tlist *tmp; | |
1cae46be | 1437 | |
4e91a871 | 1438 | for (tmp = list; tmp; tmp = tmp->next) |
1439 | { | |
1440 | if (tmp->writer) | |
1441 | warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); | |
1442 | } | |
1443 | } | |
1444 | ||
734c98be | 1445 | /* Return nonzero if X is a tree that can be verified by the sequence point |
4e91a871 | 1446 | warnings. */ |
1447 | static int | |
1cae46be | 1448 | warning_candidate_p (tree x) |
481c6ce6 | 1449 | { |
4e91a871 | 1450 | return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL; |
1451 | } | |
481c6ce6 | 1452 | |
4e91a871 | 1453 | /* Walk the tree X, and record accesses to variables. If X is written by the |
1454 | parent tree, WRITER is the parent. | |
1455 | We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this | |
1456 | expression or its only operand forces a sequence point, then everything up | |
1457 | to the sequence point is stored in PBEFORE_SP. Everything else gets stored | |
1458 | in PNO_SP. | |
1459 | Once we return, we will have emitted warnings if any subexpression before | |
1460 | such a sequence point could be undefined. On a higher level, however, the | |
1461 | sequence point may not be relevant, and we'll merge the two lists. | |
1462 | ||
1463 | Example: (b++, a) + b; | |
1464 | The call that processes the COMPOUND_EXPR will store the increment of B | |
1465 | in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that | |
1466 | processes the PLUS_EXPR will need to merge the two lists so that | |
1467 | eventually, all accesses end up on the same list (and we'll warn about the | |
1468 | unordered subexpressions b++ and b. | |
1469 | ||
1470 | A note on merging. If we modify the former example so that our expression | |
1471 | becomes | |
1472 | (b++, b) + a | |
1473 | care must be taken not simply to add all three expressions into the final | |
1474 | PNO_SP list. The function merge_tlist takes care of that by merging the | |
1475 | before-SP list of the COMPOUND_EXPR into its after-SP list in a special | |
1476 | way, so that no more than one access to B is recorded. */ | |
481c6ce6 | 1477 | |
4e91a871 | 1478 | static void |
1cae46be | 1479 | verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp, |
1480 | tree writer) | |
4e91a871 | 1481 | { |
1482 | struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; | |
1483 | enum tree_code code; | |
1484 | char class; | |
481c6ce6 | 1485 | |
e5b75768 | 1486 | /* X may be NULL if it is the operand of an empty statement expression |
1487 | ({ }). */ | |
1488 | if (x == NULL) | |
1489 | return; | |
1490 | ||
4e91a871 | 1491 | restart: |
1492 | code = TREE_CODE (x); | |
1493 | class = TREE_CODE_CLASS (code); | |
481c6ce6 | 1494 | |
4e91a871 | 1495 | if (warning_candidate_p (x)) |
481c6ce6 | 1496 | { |
4e91a871 | 1497 | *pno_sp = new_tlist (*pno_sp, x, writer); |
1498 | return; | |
1499 | } | |
1500 | ||
1501 | switch (code) | |
1502 | { | |
67b28e3e | 1503 | case CONSTRUCTOR: |
1504 | return; | |
1505 | ||
4e91a871 | 1506 | case COMPOUND_EXPR: |
1507 | case TRUTH_ANDIF_EXPR: | |
1508 | case TRUTH_ORIF_EXPR: | |
1509 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
1510 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
1511 | warn_for_collisions (tmp_nosp); | |
1512 | merge_tlist (pbefore_sp, tmp_before, 0); | |
1513 | merge_tlist (pbefore_sp, tmp_nosp, 0); | |
1514 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); | |
1515 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
1516 | return; | |
1517 | ||
1518 | case COND_EXPR: | |
1519 | tmp_before = tmp_list2 = 0; | |
1520 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); | |
1521 | warn_for_collisions (tmp_list2); | |
1522 | merge_tlist (pbefore_sp, tmp_before, 0); | |
1523 | merge_tlist (pbefore_sp, tmp_list2, 1); | |
1524 | ||
1525 | tmp_list3 = tmp_nosp = 0; | |
1526 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); | |
1527 | warn_for_collisions (tmp_nosp); | |
1528 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
1529 | ||
1530 | tmp_list3 = tmp_list2 = 0; | |
1531 | verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); | |
1532 | warn_for_collisions (tmp_list2); | |
1533 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
1534 | /* Rather than add both tmp_nosp and tmp_list2, we have to merge the | |
1535 | two first, to avoid warning for (a ? b++ : b++). */ | |
1536 | merge_tlist (&tmp_nosp, tmp_list2, 0); | |
1537 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
1538 | return; | |
1539 | ||
481c6ce6 | 1540 | case PREDECREMENT_EXPR: |
1541 | case PREINCREMENT_EXPR: | |
1542 | case POSTDECREMENT_EXPR: | |
1543 | case POSTINCREMENT_EXPR: | |
4e91a871 | 1544 | verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); |
1545 | return; | |
1546 | ||
1547 | case MODIFY_EXPR: | |
1548 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
1549 | verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); | |
1550 | verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); | |
1551 | /* Expressions inside the LHS are not ordered wrt. the sequence points | |
1552 | in the RHS. Example: | |
1553 | *a = (a++, 2) | |
1554 | Despite the fact that the modification of "a" is in the before_sp | |
1555 | list (tmp_before), it conflicts with the use of "a" in the LHS. | |
1556 | We can handle this by adding the contents of tmp_list3 | |
1557 | to those of tmp_before, and redoing the collision warnings for that | |
1558 | list. */ | |
1559 | add_tlist (&tmp_before, tmp_list3, x, 1); | |
1560 | warn_for_collisions (tmp_before); | |
1561 | /* Exclude the LHS itself here; we first have to merge it into the | |
1562 | tmp_nosp list. This is done to avoid warning for "a = a"; if we | |
1563 | didn't exclude the LHS, we'd get it twice, once as a read and once | |
1564 | as a write. */ | |
1565 | add_tlist (pno_sp, tmp_list3, x, 0); | |
1566 | warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); | |
1567 | ||
1568 | merge_tlist (pbefore_sp, tmp_before, 0); | |
1569 | if (warning_candidate_p (TREE_OPERAND (x, 0))) | |
1570 | merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); | |
1571 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); | |
1572 | return; | |
481c6ce6 | 1573 | |
1574 | case CALL_EXPR: | |
4e91a871 | 1575 | /* We need to warn about conflicts among arguments and conflicts between |
1576 | args and the function address. Side effects of the function address, | |
1577 | however, are not ordered by the sequence point of the call. */ | |
1578 | tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0; | |
1579 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
1580 | if (TREE_OPERAND (x, 1)) | |
1581 | verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE); | |
1582 | merge_tlist (&tmp_list3, tmp_list2, 0); | |
1583 | add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); | |
1584 | add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); | |
1585 | warn_for_collisions (tmp_before); | |
1586 | add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); | |
1587 | return; | |
481c6ce6 | 1588 | |
1589 | case TREE_LIST: | |
1590 | /* Scan all the list, e.g. indices of multi dimensional array. */ | |
1591 | while (x) | |
1592 | { | |
4e91a871 | 1593 | tmp_before = tmp_nosp = 0; |
1594 | verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
1595 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
1596 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
481c6ce6 | 1597 | x = TREE_CHAIN (x); |
1598 | } | |
4e91a871 | 1599 | return; |
481c6ce6 | 1600 | |
4e91a871 | 1601 | case SAVE_EXPR: |
1602 | { | |
1603 | struct tlist_cache *t; | |
1604 | for (t = save_expr_cache; t; t = t->next) | |
1605 | if (t->expr == x) | |
1606 | break; | |
481c6ce6 | 1607 | |
4e91a871 | 1608 | if (! t) |
481c6ce6 | 1609 | { |
f0af5a88 | 1610 | t = obstack_alloc (&tlist_obstack, sizeof *t); |
4e91a871 | 1611 | t->next = save_expr_cache; |
1612 | t->expr = x; | |
1613 | save_expr_cache = t; | |
1614 | ||
1615 | tmp_before = tmp_nosp = 0; | |
1616 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
1617 | warn_for_collisions (tmp_nosp); | |
1618 | ||
1619 | tmp_list3 = 0; | |
1620 | while (tmp_nosp) | |
1621 | { | |
1622 | struct tlist *t = tmp_nosp; | |
1623 | tmp_nosp = t->next; | |
1624 | merge_tlist (&tmp_list3, t, 0); | |
1625 | } | |
1626 | t->cache_before_sp = tmp_before; | |
1627 | t->cache_after_sp = tmp_list3; | |
481c6ce6 | 1628 | } |
4e91a871 | 1629 | merge_tlist (pbefore_sp, t->cache_before_sp, 1); |
1630 | add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); | |
1631 | return; | |
1632 | } | |
1633 | default: | |
481c6ce6 | 1634 | break; |
1635 | } | |
481c6ce6 | 1636 | |
4e91a871 | 1637 | if (class == '1') |
481c6ce6 | 1638 | { |
4e91a871 | 1639 | if (first_rtl_op (code) == 0) |
1640 | return; | |
1641 | x = TREE_OPERAND (x, 0); | |
1642 | writer = 0; | |
1643 | goto restart; | |
481c6ce6 | 1644 | } |
1645 | ||
4e91a871 | 1646 | switch (class) |
481c6ce6 | 1647 | { |
4e91a871 | 1648 | case 'r': |
1649 | case '<': | |
1650 | case '2': | |
1651 | case 'b': | |
1652 | case 'e': | |
1653 | case 's': | |
1654 | case 'x': | |
1655 | { | |
1656 | int lp; | |
1657 | int max = first_rtl_op (TREE_CODE (x)); | |
1658 | for (lp = 0; lp < max; lp++) | |
1659 | { | |
1660 | tmp_before = tmp_nosp = 0; | |
1661 | verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE); | |
1662 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
1663 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
1664 | } | |
1665 | break; | |
1666 | } | |
481c6ce6 | 1667 | } |
481c6ce6 | 1668 | } |
1669 | ||
974e2c0c | 1670 | /* Try to warn for undefined behavior in EXPR due to missing sequence |
481c6ce6 | 1671 | points. */ |
1672 | ||
1673 | static void | |
1cae46be | 1674 | verify_sequence_points (tree expr) |
481c6ce6 | 1675 | { |
4e91a871 | 1676 | struct tlist *before_sp = 0, *after_sp = 0; |
481c6ce6 | 1677 | |
4e91a871 | 1678 | warned_ids = 0; |
1679 | save_expr_cache = 0; | |
1680 | if (tlist_firstobj == 0) | |
481c6ce6 | 1681 | { |
4e91a871 | 1682 | gcc_obstack_init (&tlist_obstack); |
1683 | tlist_firstobj = obstack_alloc (&tlist_obstack, 0); | |
481c6ce6 | 1684 | } |
1685 | ||
4e91a871 | 1686 | verify_tree (expr, &before_sp, &after_sp, 0); |
1687 | warn_for_collisions (after_sp); | |
1688 | obstack_free (&tlist_obstack, tlist_firstobj); | |
481c6ce6 | 1689 | } |
1690 | ||
7cad36f4 | 1691 | tree |
1cae46be | 1692 | c_expand_expr_stmt (tree expr) |
b0fc3e72 | 1693 | { |
1694 | /* Do default conversion if safe and possibly important, | |
1695 | in case within ({...}). */ | |
62827f44 | 1696 | if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE |
1697 | && (flag_isoc99 || lvalue_p (expr))) | |
b0fc3e72 | 1698 | || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE) |
1699 | expr = default_conversion (expr); | |
1700 | ||
481c6ce6 | 1701 | if (warn_sequence_point) |
1702 | verify_sequence_points (expr); | |
1703 | ||
b0fc3e72 | 1704 | if (TREE_TYPE (expr) != error_mark_node |
5fd5d738 | 1705 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) |
b0fc3e72 | 1706 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) |
1707 | error ("expression statement has incomplete type"); | |
1708 | ||
1cae46be | 1709 | last_expr_type = TREE_TYPE (expr); |
7cad36f4 | 1710 | return add_stmt (build_stmt (EXPR_STMT, expr)); |
b0fc3e72 | 1711 | } |
1712 | \f | |
1713 | /* Validate the expression after `case' and apply default promotions. */ | |
1714 | ||
1715 | tree | |
1cae46be | 1716 | check_case_value (tree value) |
b0fc3e72 | 1717 | { |
1718 | if (value == NULL_TREE) | |
1719 | return value; | |
1720 | ||
1721 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
fce1d6af | 1722 | STRIP_TYPE_NOPS (value); |
225ec6aa | 1723 | /* In C++, the following is allowed: |
1724 | ||
1725 | const int i = 3; | |
1726 | switch (...) { case i: ... } | |
1727 | ||
1728 | So, we try to reduce the VALUE to a constant that way. */ | |
c0f19401 | 1729 | if (c_dialect_cxx ()) |
225ec6aa | 1730 | { |
1731 | value = decl_constant_value (value); | |
1732 | STRIP_TYPE_NOPS (value); | |
1733 | value = fold (value); | |
1734 | } | |
b0fc3e72 | 1735 | |
1736 | if (TREE_CODE (value) != INTEGER_CST | |
1737 | && value != error_mark_node) | |
1738 | { | |
1739 | error ("case label does not reduce to an integer constant"); | |
1740 | value = error_mark_node; | |
1741 | } | |
1742 | else | |
1743 | /* Promote char or short to int. */ | |
1744 | value = default_conversion (value); | |
1745 | ||
6433f1c2 | 1746 | constant_expression_warning (value); |
1747 | ||
b0fc3e72 | 1748 | return value; |
1749 | } | |
1750 | \f | |
1751 | /* Return an integer type with BITS bits of precision, | |
1752 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
1753 | ||
1754 | tree | |
1cae46be | 1755 | c_common_type_for_size (unsigned int bits, int unsignedp) |
b0fc3e72 | 1756 | { |
46375237 | 1757 | if (bits == TYPE_PRECISION (integer_type_node)) |
1758 | return unsignedp ? unsigned_type_node : integer_type_node; | |
1759 | ||
bacde65a | 1760 | if (bits == TYPE_PRECISION (signed_char_type_node)) |
b0fc3e72 | 1761 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
1762 | ||
bacde65a | 1763 | if (bits == TYPE_PRECISION (short_integer_type_node)) |
b0fc3e72 | 1764 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
1765 | ||
bacde65a | 1766 | if (bits == TYPE_PRECISION (long_integer_type_node)) |
b0fc3e72 | 1767 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
1768 | ||
bacde65a | 1769 | if (bits == TYPE_PRECISION (long_long_integer_type_node)) |
b0fc3e72 | 1770 | return (unsignedp ? long_long_unsigned_type_node |
1771 | : long_long_integer_type_node); | |
1772 | ||
f57fa2ea | 1773 | if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) |
1774 | return (unsignedp ? widest_unsigned_literal_type_node | |
1775 | : widest_integer_literal_type_node); | |
1776 | ||
bacde65a | 1777 | if (bits <= TYPE_PRECISION (intQI_type_node)) |
1778 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
1779 | ||
1780 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
1781 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
1782 | ||
1783 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
1784 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
1785 | ||
1786 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
1787 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
1788 | ||
b0fc3e72 | 1789 | return 0; |
1790 | } | |
1791 | ||
1792 | /* Return a data type that has machine mode MODE. | |
1793 | If the mode is an integer, | |
1794 | then UNSIGNEDP selects between signed and unsigned types. */ | |
1795 | ||
1796 | tree | |
1cae46be | 1797 | c_common_type_for_mode (enum machine_mode mode, int unsignedp) |
b0fc3e72 | 1798 | { |
46375237 | 1799 | if (mode == TYPE_MODE (integer_type_node)) |
1800 | return unsignedp ? unsigned_type_node : integer_type_node; | |
1801 | ||
b0fc3e72 | 1802 | if (mode == TYPE_MODE (signed_char_type_node)) |
1803 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
1804 | ||
1805 | if (mode == TYPE_MODE (short_integer_type_node)) | |
1806 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
1807 | ||
b0fc3e72 | 1808 | if (mode == TYPE_MODE (long_integer_type_node)) |
1809 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
1810 | ||
1811 | if (mode == TYPE_MODE (long_long_integer_type_node)) | |
1812 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
1813 | ||
f57fa2ea | 1814 | if (mode == TYPE_MODE (widest_integer_literal_type_node)) |
44e9fa65 | 1815 | return unsignedp ? widest_unsigned_literal_type_node |
f57fa2ea | 1816 | : widest_integer_literal_type_node; |
1817 | ||
88ae7f04 | 1818 | if (mode == QImode) |
bacde65a | 1819 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
1820 | ||
88ae7f04 | 1821 | if (mode == HImode) |
bacde65a | 1822 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
1823 | ||
88ae7f04 | 1824 | if (mode == SImode) |
bacde65a | 1825 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
1826 | ||
88ae7f04 | 1827 | if (mode == DImode) |
bacde65a | 1828 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
1829 | ||
cc1cc1c7 | 1830 | #if HOST_BITS_PER_WIDE_INT >= 64 |
6274009c | 1831 | if (mode == TYPE_MODE (intTI_type_node)) |
1832 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
cc1cc1c7 | 1833 | #endif |
6274009c | 1834 | |
b0fc3e72 | 1835 | if (mode == TYPE_MODE (float_type_node)) |
1836 | return float_type_node; | |
1837 | ||
1838 | if (mode == TYPE_MODE (double_type_node)) | |
1839 | return double_type_node; | |
1840 | ||
1841 | if (mode == TYPE_MODE (long_double_type_node)) | |
1842 | return long_double_type_node; | |
1843 | ||
1844 | if (mode == TYPE_MODE (build_pointer_type (char_type_node))) | |
4eed1316 | 1845 | return unsignedp ? make_unsigned_type (mode) : make_signed_type (mode); |
b0fc3e72 | 1846 | |
1847 | if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) | |
4eed1316 | 1848 | return unsignedp ? make_unsigned_type (mode) : make_signed_type (mode); |
b0fc3e72 | 1849 | |
ead34f59 | 1850 | switch (mode) |
1851 | { | |
1852 | case V16QImode: | |
1853 | return unsignedp ? unsigned_V16QI_type_node : V16QI_type_node; | |
1854 | case V8HImode: | |
1855 | return unsignedp ? unsigned_V8HI_type_node : V8HI_type_node; | |
1856 | case V4SImode: | |
1857 | return unsignedp ? unsigned_V4SI_type_node : V4SI_type_node; | |
1858 | case V2DImode: | |
1859 | return unsignedp ? unsigned_V2DI_type_node : V2DI_type_node; | |
1860 | case V2SImode: | |
1861 | return unsignedp ? unsigned_V2SI_type_node : V2SI_type_node; | |
50abdc96 | 1862 | case V2HImode: |
1863 | return unsignedp ? unsigned_V2HI_type_node : V2HI_type_node; | |
ead34f59 | 1864 | case V4HImode: |
1865 | return unsignedp ? unsigned_V4HI_type_node : V4HI_type_node; | |
1866 | case V8QImode: | |
1867 | return unsignedp ? unsigned_V8QI_type_node : V8QI_type_node; | |
d33e2bd3 | 1868 | case V1DImode: |
1869 | return unsignedp ? unsigned_V1DI_type_node : V1DI_type_node; | |
ead34f59 | 1870 | case V16SFmode: |
1871 | return V16SF_type_node; | |
1872 | case V4SFmode: | |
1873 | return V4SF_type_node; | |
1874 | case V2SFmode: | |
1875 | return V2SF_type_node; | |
1876 | case V2DFmode: | |
1877 | return V2DF_type_node; | |
8788f673 | 1878 | case V4DFmode: |
1879 | return V4DF_type_node; | |
ead34f59 | 1880 | default: |
1881 | break; | |
88ae7f04 | 1882 | } |
e2ea7e3a | 1883 | |
b0fc3e72 | 1884 | return 0; |
1885 | } | |
20d39783 | 1886 | |
2c0e001b | 1887 | /* Return an unsigned type the same as TYPE in other respects. */ |
20d39783 | 1888 | tree |
1cae46be | 1889 | c_common_unsigned_type (tree type) |
20d39783 | 1890 | { |
1891 | tree type1 = TYPE_MAIN_VARIANT (type); | |
1892 | if (type1 == signed_char_type_node || type1 == char_type_node) | |
1893 | return unsigned_char_type_node; | |
1894 | if (type1 == integer_type_node) | |
1895 | return unsigned_type_node; | |
1896 | if (type1 == short_integer_type_node) | |
1897 | return short_unsigned_type_node; | |
1898 | if (type1 == long_integer_type_node) | |
1899 | return long_unsigned_type_node; | |
1900 | if (type1 == long_long_integer_type_node) | |
1901 | return long_long_unsigned_type_node; | |
1902 | if (type1 == widest_integer_literal_type_node) | |
1903 | return widest_unsigned_literal_type_node; | |
1904 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
1905 | if (type1 == intTI_type_node) | |
1906 | return unsigned_intTI_type_node; | |
1907 | #endif | |
1908 | if (type1 == intDI_type_node) | |
1909 | return unsigned_intDI_type_node; | |
1910 | if (type1 == intSI_type_node) | |
1911 | return unsigned_intSI_type_node; | |
1912 | if (type1 == intHI_type_node) | |
1913 | return unsigned_intHI_type_node; | |
1914 | if (type1 == intQI_type_node) | |
1915 | return unsigned_intQI_type_node; | |
1916 | ||
4070745f | 1917 | return c_common_signed_or_unsigned_type (1, type); |
20d39783 | 1918 | } |
1919 | ||
1920 | /* Return a signed type the same as TYPE in other respects. */ | |
1921 | ||
1922 | tree | |
1cae46be | 1923 | c_common_signed_type (tree type) |
20d39783 | 1924 | { |
1925 | tree type1 = TYPE_MAIN_VARIANT (type); | |
1926 | if (type1 == unsigned_char_type_node || type1 == char_type_node) | |
1927 | return signed_char_type_node; | |
1928 | if (type1 == unsigned_type_node) | |
1929 | return integer_type_node; | |
1930 | if (type1 == short_unsigned_type_node) | |
1931 | return short_integer_type_node; | |
1932 | if (type1 == long_unsigned_type_node) | |
1933 | return long_integer_type_node; | |
1934 | if (type1 == long_long_unsigned_type_node) | |
1935 | return long_long_integer_type_node; | |
1936 | if (type1 == widest_unsigned_literal_type_node) | |
1937 | return widest_integer_literal_type_node; | |
1938 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
1939 | if (type1 == unsigned_intTI_type_node) | |
1940 | return intTI_type_node; | |
1941 | #endif | |
1942 | if (type1 == unsigned_intDI_type_node) | |
1943 | return intDI_type_node; | |
1944 | if (type1 == unsigned_intSI_type_node) | |
1945 | return intSI_type_node; | |
1946 | if (type1 == unsigned_intHI_type_node) | |
1947 | return intHI_type_node; | |
1948 | if (type1 == unsigned_intQI_type_node) | |
1949 | return intQI_type_node; | |
1950 | ||
4070745f | 1951 | return c_common_signed_or_unsigned_type (0, type); |
20d39783 | 1952 | } |
1953 | ||
1954 | /* Return a type the same as TYPE except unsigned or | |
1955 | signed according to UNSIGNEDP. */ | |
1956 | ||
1957 | tree | |
1cae46be | 1958 | c_common_signed_or_unsigned_type (int unsignedp, tree type) |
20d39783 | 1959 | { |
1960 | if (! INTEGRAL_TYPE_P (type) | |
1961 | || TREE_UNSIGNED (type) == unsignedp) | |
1962 | return type; | |
1963 | ||
1964 | if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)) | |
1965 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
44e9fa65 | 1966 | if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
20d39783 | 1967 | return unsignedp ? unsigned_type_node : integer_type_node; |
44e9fa65 | 1968 | if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node)) |
20d39783 | 1969 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
44e9fa65 | 1970 | if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node)) |
20d39783 | 1971 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
44e9fa65 | 1972 | if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node)) |
20d39783 | 1973 | return (unsignedp ? long_long_unsigned_type_node |
1974 | : long_long_integer_type_node); | |
44e9fa65 | 1975 | if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node)) |
20d39783 | 1976 | return (unsignedp ? widest_unsigned_literal_type_node |
1977 | : widest_integer_literal_type_node); | |
ef11801e | 1978 | |
1979 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
1980 | if (TYPE_PRECISION (type) == TYPE_PRECISION (intTI_type_node)) | |
1981 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
1982 | #endif | |
1983 | if (TYPE_PRECISION (type) == TYPE_PRECISION (intDI_type_node)) | |
1984 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
1985 | if (TYPE_PRECISION (type) == TYPE_PRECISION (intSI_type_node)) | |
1986 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
1987 | if (TYPE_PRECISION (type) == TYPE_PRECISION (intHI_type_node)) | |
1988 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
1989 | if (TYPE_PRECISION (type) == TYPE_PRECISION (intQI_type_node)) | |
1990 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
1991 | ||
20d39783 | 1992 | return type; |
1993 | } | |
b0fc3e72 | 1994 | \f |
a9b9d10c | 1995 | /* Return the minimum number of bits needed to represent VALUE in a |
1996 | signed or unsigned type, UNSIGNEDP says which. */ | |
1997 | ||
a0c2c45b | 1998 | unsigned int |
1cae46be | 1999 | min_precision (tree value, int unsignedp) |
a9b9d10c | 2000 | { |
2001 | int log; | |
2002 | ||
2003 | /* If the value is negative, compute its negative minus 1. The latter | |
2004 | adjustment is because the absolute value of the largest negative value | |
2005 | is one larger than the largest positive value. This is equivalent to | |
2006 | a bit-wise negation, so use that operation instead. */ | |
2007 | ||
2008 | if (tree_int_cst_sgn (value) < 0) | |
2009 | value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value)); | |
2010 | ||
2011 | /* Return the number of bits needed, taking into account the fact | |
2012 | that we need one more bit for a signed than unsigned type. */ | |
2013 | ||
2014 | if (integer_zerop (value)) | |
2015 | log = 0; | |
a9b9d10c | 2016 | else |
a0c2c45b | 2017 | log = tree_floor_log2 (value); |
a9b9d10c | 2018 | |
2019 | return log + 1 + ! unsignedp; | |
2020 | } | |
2021 | \f | |
aff9e656 | 2022 | /* Print an error message for invalid operands to arith operation |
2023 | CODE. NOP_EXPR is used as a special case (see | |
2024 | c_common_truthvalue_conversion). */ | |
b0fc3e72 | 2025 | |
2026 | void | |
1cae46be | 2027 | binary_op_error (enum tree_code code) |
b0fc3e72 | 2028 | { |
19cb6b50 | 2029 | const char *opname; |
f03946e4 | 2030 | |
b0fc3e72 | 2031 | switch (code) |
2032 | { | |
2033 | case NOP_EXPR: | |
2034 | error ("invalid truth-value expression"); | |
2035 | return; | |
2036 | ||
2037 | case PLUS_EXPR: | |
2038 | opname = "+"; break; | |
2039 | case MINUS_EXPR: | |
2040 | opname = "-"; break; | |
2041 | case MULT_EXPR: | |
2042 | opname = "*"; break; | |
2043 | case MAX_EXPR: | |
2044 | opname = "max"; break; | |
2045 | case MIN_EXPR: | |
2046 | opname = "min"; break; | |
2047 | case EQ_EXPR: | |
2048 | opname = "=="; break; | |
2049 | case NE_EXPR: | |
2050 | opname = "!="; break; | |
2051 | case LE_EXPR: | |
2052 | opname = "<="; break; | |
2053 | case GE_EXPR: | |
2054 | opname = ">="; break; | |
2055 | case LT_EXPR: | |
2056 | opname = "<"; break; | |
2057 | case GT_EXPR: | |
2058 | opname = ">"; break; | |
2059 | case LSHIFT_EXPR: | |
2060 | opname = "<<"; break; | |
2061 | case RSHIFT_EXPR: | |
2062 | opname = ">>"; break; | |
2063 | case TRUNC_MOD_EXPR: | |
66618a1e | 2064 | case FLOOR_MOD_EXPR: |
b0fc3e72 | 2065 | opname = "%"; break; |
2066 | case TRUNC_DIV_EXPR: | |
66618a1e | 2067 | case FLOOR_DIV_EXPR: |
b0fc3e72 | 2068 | opname = "/"; break; |
2069 | case BIT_AND_EXPR: | |
2070 | opname = "&"; break; | |
2071 | case BIT_IOR_EXPR: | |
2072 | opname = "|"; break; | |
2073 | case TRUTH_ANDIF_EXPR: | |
2074 | opname = "&&"; break; | |
2075 | case TRUTH_ORIF_EXPR: | |
2076 | opname = "||"; break; | |
2077 | case BIT_XOR_EXPR: | |
2078 | opname = "^"; break; | |
66618a1e | 2079 | case LROTATE_EXPR: |
2080 | case RROTATE_EXPR: | |
2081 | opname = "rotate"; break; | |
31f820d2 | 2082 | default: |
2083 | opname = "unknown"; break; | |
b0fc3e72 | 2084 | } |
2085 | error ("invalid operands to binary %s", opname); | |
2086 | } | |
2087 | \f | |
2088 | /* Subroutine of build_binary_op, used for comparison operations. | |
2089 | See if the operands have both been converted from subword integer types | |
2090 | and, if so, perhaps change them both back to their original type. | |
5b511807 | 2091 | This function is also responsible for converting the two operands |
2092 | to the proper common type for comparison. | |
b0fc3e72 | 2093 | |
2094 | The arguments of this function are all pointers to local variables | |
2095 | of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, | |
2096 | RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. | |
2097 | ||
2098 | If this function returns nonzero, it means that the comparison has | |
2099 | a constant value. What this function returns is an expression for | |
2100 | that value. */ | |
2101 | ||
2102 | tree | |
1cae46be | 2103 | shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr, |
2104 | enum tree_code *rescode_ptr) | |
b0fc3e72 | 2105 | { |
19cb6b50 | 2106 | tree type; |
b0fc3e72 | 2107 | tree op0 = *op0_ptr; |
2108 | tree op1 = *op1_ptr; | |
2109 | int unsignedp0, unsignedp1; | |
2110 | int real1, real2; | |
2111 | tree primop0, primop1; | |
2112 | enum tree_code code = *rescode_ptr; | |
2113 | ||
2114 | /* Throw away any conversions to wider types | |
2115 | already present in the operands. */ | |
2116 | ||
2117 | primop0 = get_narrower (op0, &unsignedp0); | |
2118 | primop1 = get_narrower (op1, &unsignedp1); | |
2119 | ||
2120 | /* Handle the case that OP0 does not *contain* a conversion | |
2121 | but it *requires* conversion to FINAL_TYPE. */ | |
2122 | ||
2123 | if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) | |
2124 | unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0)); | |
2125 | if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) | |
2126 | unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1)); | |
2127 | ||
2128 | /* If one of the operands must be floated, we cannot optimize. */ | |
2129 | real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; | |
2130 | real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; | |
2131 | ||
2132 | /* If first arg is constant, swap the args (changing operation | |
2bd278cc | 2133 | so value is preserved), for canonicalization. Don't do this if |
2134 | the second arg is 0. */ | |
b0fc3e72 | 2135 | |
2bd278cc | 2136 | if (TREE_CONSTANT (primop0) |
2137 | && ! integer_zerop (primop1) && ! real_zerop (primop1)) | |
b0fc3e72 | 2138 | { |
19cb6b50 | 2139 | tree tem = primop0; |
2140 | int temi = unsignedp0; | |
b0fc3e72 | 2141 | primop0 = primop1; |
2142 | primop1 = tem; | |
2143 | tem = op0; | |
2144 | op0 = op1; | |
2145 | op1 = tem; | |
2146 | *op0_ptr = op0; | |
2147 | *op1_ptr = op1; | |
2148 | unsignedp0 = unsignedp1; | |
2149 | unsignedp1 = temi; | |
2150 | temi = real1; | |
2151 | real1 = real2; | |
2152 | real2 = temi; | |
2153 | ||
2154 | switch (code) | |
2155 | { | |
2156 | case LT_EXPR: | |
2157 | code = GT_EXPR; | |
2158 | break; | |
2159 | case GT_EXPR: | |
2160 | code = LT_EXPR; | |
2161 | break; | |
2162 | case LE_EXPR: | |
2163 | code = GE_EXPR; | |
2164 | break; | |
2165 | case GE_EXPR: | |
2166 | code = LE_EXPR; | |
2167 | break; | |
31f820d2 | 2168 | default: |
2169 | break; | |
b0fc3e72 | 2170 | } |
2171 | *rescode_ptr = code; | |
2172 | } | |
2173 | ||
2174 | /* If comparing an integer against a constant more bits wide, | |
2175 | maybe we can deduce a value of 1 or 0 independent of the data. | |
2176 | Or else truncate the constant now | |
2177 | rather than extend the variable at run time. | |
2178 | ||
2179 | This is only interesting if the constant is the wider arg. | |
2180 | Also, it is not safe if the constant is unsigned and the | |
2181 | variable arg is signed, since in this case the variable | |
2182 | would be sign-extended and then regarded as unsigned. | |
2183 | Our technique fails in this case because the lowest/highest | |
2184 | possible unsigned results don't follow naturally from the | |
2185 | lowest/highest possible values of the variable operand. | |
2186 | For just EQ_EXPR and NE_EXPR there is another technique that | |
2187 | could be used: see if the constant can be faithfully represented | |
2188 | in the other operand's type, by truncating it and reextending it | |
2189 | and see if that preserves the constant's value. */ | |
2190 | ||
2191 | if (!real1 && !real2 | |
2192 | && TREE_CODE (primop1) == INTEGER_CST | |
2193 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) | |
2194 | { | |
2195 | int min_gt, max_gt, min_lt, max_lt; | |
2196 | tree maxval, minval; | |
2197 | /* 1 if comparison is nominally unsigned. */ | |
2198 | int unsignedp = TREE_UNSIGNED (*restype_ptr); | |
2199 | tree val; | |
2200 | ||
4070745f | 2201 | type = c_common_signed_or_unsigned_type (unsignedp0, |
2202 | TREE_TYPE (primop0)); | |
cda09c61 | 2203 | |
ee1ab431 | 2204 | /* In C, if TYPE is an enumeration, then we need to get its |
2205 | min/max values from it's underlying integral type, not the | |
2206 | enumerated type itself. In C++, TYPE_MAX_VALUE and | |
2207 | TYPE_MIN_VALUE have already been set correctly on the | |
2208 | enumeration type. */ | |
2209 | if (!c_dialect_cxx() && TREE_CODE (type) == ENUMERAL_TYPE) | |
771d21fa | 2210 | type = c_common_type_for_size (TYPE_PRECISION (type), unsignedp0); |
b0fc3e72 | 2211 | |
2212 | maxval = TYPE_MAX_VALUE (type); | |
2213 | minval = TYPE_MIN_VALUE (type); | |
2214 | ||
2215 | if (unsignedp && !unsignedp0) | |
4070745f | 2216 | *restype_ptr = c_common_signed_type (*restype_ptr); |
b0fc3e72 | 2217 | |
2218 | if (TREE_TYPE (primop1) != *restype_ptr) | |
2219 | primop1 = convert (*restype_ptr, primop1); | |
2220 | if (type != *restype_ptr) | |
2221 | { | |
2222 | minval = convert (*restype_ptr, minval); | |
2223 | maxval = convert (*restype_ptr, maxval); | |
2224 | } | |
2225 | ||
2226 | if (unsignedp && unsignedp0) | |
2227 | { | |
2228 | min_gt = INT_CST_LT_UNSIGNED (primop1, minval); | |
2229 | max_gt = INT_CST_LT_UNSIGNED (primop1, maxval); | |
2230 | min_lt = INT_CST_LT_UNSIGNED (minval, primop1); | |
2231 | max_lt = INT_CST_LT_UNSIGNED (maxval, primop1); | |
2232 | } | |
2233 | else | |
2234 | { | |
2235 | min_gt = INT_CST_LT (primop1, minval); | |
2236 | max_gt = INT_CST_LT (primop1, maxval); | |
2237 | min_lt = INT_CST_LT (minval, primop1); | |
2238 | max_lt = INT_CST_LT (maxval, primop1); | |
2239 | } | |
2240 | ||
2241 | val = 0; | |
2242 | /* This used to be a switch, but Genix compiler can't handle that. */ | |
2243 | if (code == NE_EXPR) | |
2244 | { | |
2245 | if (max_lt || min_gt) | |
3c2239cf | 2246 | val = truthvalue_true_node; |
b0fc3e72 | 2247 | } |
2248 | else if (code == EQ_EXPR) | |
2249 | { | |
2250 | if (max_lt || min_gt) | |
3c2239cf | 2251 | val = truthvalue_false_node; |
b0fc3e72 | 2252 | } |
2253 | else if (code == LT_EXPR) | |
2254 | { | |
2255 | if (max_lt) | |
3c2239cf | 2256 | val = truthvalue_true_node; |
b0fc3e72 | 2257 | if (!min_lt) |
3c2239cf | 2258 | val = truthvalue_false_node; |
b0fc3e72 | 2259 | } |
2260 | else if (code == GT_EXPR) | |
2261 | { | |
2262 | if (min_gt) | |
3c2239cf | 2263 | val = truthvalue_true_node; |
b0fc3e72 | 2264 | if (!max_gt) |
3c2239cf | 2265 | val = truthvalue_false_node; |
b0fc3e72 | 2266 | } |
2267 | else if (code == LE_EXPR) | |
2268 | { | |
2269 | if (!max_gt) | |
3c2239cf | 2270 | val = truthvalue_true_node; |
b0fc3e72 | 2271 | if (min_gt) |
3c2239cf | 2272 | val = truthvalue_false_node; |
b0fc3e72 | 2273 | } |
2274 | else if (code == GE_EXPR) | |
2275 | { | |
2276 | if (!min_lt) | |
3c2239cf | 2277 | val = truthvalue_true_node; |
b0fc3e72 | 2278 | if (max_lt) |
3c2239cf | 2279 | val = truthvalue_false_node; |
b0fc3e72 | 2280 | } |
2281 | ||
2282 | /* If primop0 was sign-extended and unsigned comparison specd, | |
2283 | we did a signed comparison above using the signed type bounds. | |
2284 | But the comparison we output must be unsigned. | |
2285 | ||
2286 | Also, for inequalities, VAL is no good; but if the signed | |
2287 | comparison had *any* fixed result, it follows that the | |
2288 | unsigned comparison just tests the sign in reverse | |
2289 | (positive values are LE, negative ones GE). | |
2290 | So we can generate an unsigned comparison | |
2291 | against an extreme value of the signed type. */ | |
2292 | ||
2293 | if (unsignedp && !unsignedp0) | |
2294 | { | |
2295 | if (val != 0) | |
2296 | switch (code) | |
2297 | { | |
2298 | case LT_EXPR: | |
2299 | case GE_EXPR: | |
2300 | primop1 = TYPE_MIN_VALUE (type); | |
2301 | val = 0; | |
2302 | break; | |
2303 | ||
2304 | case LE_EXPR: | |
2305 | case GT_EXPR: | |
2306 | primop1 = TYPE_MAX_VALUE (type); | |
2307 | val = 0; | |
2308 | break; | |
31f820d2 | 2309 | |
2310 | default: | |
2311 | break; | |
b0fc3e72 | 2312 | } |
4070745f | 2313 | type = c_common_unsigned_type (type); |
b0fc3e72 | 2314 | } |
2315 | ||
fe5f2366 | 2316 | if (TREE_CODE (primop0) != INTEGER_CST) |
b0fc3e72 | 2317 | { |
3c2239cf | 2318 | if (val == truthvalue_false_node) |
9a64a879 | 2319 | warning ("comparison is always false due to limited range of data type"); |
3c2239cf | 2320 | if (val == truthvalue_true_node) |
9a64a879 | 2321 | warning ("comparison is always true due to limited range of data type"); |
b0fc3e72 | 2322 | } |
2323 | ||
2324 | if (val != 0) | |
2325 | { | |
2326 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
2327 | if (TREE_SIDE_EFFECTS (primop0)) | |
2328 | return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); | |
2329 | return val; | |
2330 | } | |
2331 | ||
2332 | /* Value is not predetermined, but do the comparison | |
2333 | in the type of the operand that is not constant. | |
2334 | TYPE is already properly set. */ | |
2335 | } | |
2336 | else if (real1 && real2 | |
2203bd5c | 2337 | && (TYPE_PRECISION (TREE_TYPE (primop0)) |
2338 | == TYPE_PRECISION (TREE_TYPE (primop1)))) | |
b0fc3e72 | 2339 | type = TREE_TYPE (primop0); |
2340 | ||
2341 | /* If args' natural types are both narrower than nominal type | |
2342 | and both extend in the same manner, compare them | |
2343 | in the type of the wider arg. | |
2344 | Otherwise must actually extend both to the nominal | |
2345 | common type lest different ways of extending | |
2346 | alter the result. | |
2347 | (eg, (short)-1 == (unsigned short)-1 should be 0.) */ | |
2348 | ||
2349 | else if (unsignedp0 == unsignedp1 && real1 == real2 | |
2350 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) | |
2351 | && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) | |
2352 | { | |
2353 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4070745f | 2354 | type = c_common_signed_or_unsigned_type (unsignedp0 |
2355 | || TREE_UNSIGNED (*restype_ptr), | |
2356 | type); | |
b0fc3e72 | 2357 | /* Make sure shorter operand is extended the right way |
2358 | to match the longer operand. */ | |
4070745f | 2359 | primop0 |
2360 | = convert (c_common_signed_or_unsigned_type (unsignedp0, | |
2361 | TREE_TYPE (primop0)), | |
2362 | primop0); | |
2363 | primop1 | |
2364 | = convert (c_common_signed_or_unsigned_type (unsignedp1, | |
2365 | TREE_TYPE (primop1)), | |
2366 | primop1); | |
b0fc3e72 | 2367 | } |
2368 | else | |
2369 | { | |
2370 | /* Here we must do the comparison on the nominal type | |
2371 | using the args exactly as we received them. */ | |
2372 | type = *restype_ptr; | |
2373 | primop0 = op0; | |
2374 | primop1 = op1; | |
2375 | ||
2376 | if (!real1 && !real2 && integer_zerop (primop1) | |
32ab5039 | 2377 | && TREE_UNSIGNED (*restype_ptr)) |
b0fc3e72 | 2378 | { |
2379 | tree value = 0; | |
2380 | switch (code) | |
2381 | { | |
2382 | case GE_EXPR: | |
2bd278cc | 2383 | /* All unsigned values are >= 0, so we warn if extra warnings |
2384 | are requested. However, if OP0 is a constant that is | |
2385 | >= 0, the signedness of the comparison isn't an issue, | |
2386 | so suppress the warning. */ | |
da99cd78 | 2387 | if (extra_warnings && !in_system_header |
2bd278cc | 2388 | && ! (TREE_CODE (primop0) == INTEGER_CST |
4070745f | 2389 | && ! TREE_OVERFLOW (convert (c_common_signed_type (type), |
2bd278cc | 2390 | primop0)))) |
9a64a879 | 2391 | warning ("comparison of unsigned expression >= 0 is always true"); |
3c2239cf | 2392 | value = truthvalue_true_node; |
b0fc3e72 | 2393 | break; |
2394 | ||
2395 | case LT_EXPR: | |
da99cd78 | 2396 | if (extra_warnings && !in_system_header |
2bd278cc | 2397 | && ! (TREE_CODE (primop0) == INTEGER_CST |
4070745f | 2398 | && ! TREE_OVERFLOW (convert (c_common_signed_type (type), |
2bd278cc | 2399 | primop0)))) |
9a64a879 | 2400 | warning ("comparison of unsigned expression < 0 is always false"); |
3c2239cf | 2401 | value = truthvalue_false_node; |
31f820d2 | 2402 | break; |
2403 | ||
2404 | default: | |
2405 | break; | |
b0fc3e72 | 2406 | } |
2407 | ||
2408 | if (value != 0) | |
2409 | { | |
2410 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
2411 | if (TREE_SIDE_EFFECTS (primop0)) | |
2412 | return build (COMPOUND_EXPR, TREE_TYPE (value), | |
2413 | primop0, value); | |
2414 | return value; | |
2415 | } | |
2416 | } | |
2417 | } | |
2418 | ||
2419 | *op0_ptr = convert (type, primop0); | |
2420 | *op1_ptr = convert (type, primop1); | |
2421 | ||
3c2239cf | 2422 | *restype_ptr = truthvalue_type_node; |
b0fc3e72 | 2423 | |
2424 | return 0; | |
2425 | } | |
2426 | \f | |
1c26100f | 2427 | /* Return a tree for the sum or difference (RESULTCODE says which) |
2428 | of pointer PTROP and integer INTOP. */ | |
2429 | ||
2430 | tree | |
1cae46be | 2431 | pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop) |
1c26100f | 2432 | { |
2433 | tree size_exp; | |
2434 | ||
2435 | tree result; | |
2436 | tree folded; | |
2437 | ||
2438 | /* The result is a pointer of the same type that is being added. */ | |
2439 | ||
2440 | tree result_type = TREE_TYPE (ptrop); | |
2441 | ||
2442 | if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) | |
2443 | { | |
2444 | if (pedantic || warn_pointer_arith) | |
2445 | pedwarn ("pointer of type `void *' used in arithmetic"); | |
2446 | size_exp = integer_one_node; | |
2447 | } | |
2448 | else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) | |
2449 | { | |
2450 | if (pedantic || warn_pointer_arith) | |
2451 | pedwarn ("pointer to a function used in arithmetic"); | |
2452 | size_exp = integer_one_node; | |
2453 | } | |
2454 | else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE) | |
2455 | { | |
2456 | if (pedantic || warn_pointer_arith) | |
2457 | pedwarn ("pointer to member function used in arithmetic"); | |
2458 | size_exp = integer_one_node; | |
2459 | } | |
2460 | else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE) | |
2461 | { | |
2462 | if (pedantic || warn_pointer_arith) | |
2463 | pedwarn ("pointer to a member used in arithmetic"); | |
2464 | size_exp = integer_one_node; | |
2465 | } | |
2466 | else | |
2467 | size_exp = size_in_bytes (TREE_TYPE (result_type)); | |
2468 | ||
2469 | /* If what we are about to multiply by the size of the elements | |
2470 | contains a constant term, apply distributive law | |
2471 | and multiply that constant term separately. | |
2472 | This helps produce common subexpressions. */ | |
2473 | ||
2474 | if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) | |
2475 | && ! TREE_CONSTANT (intop) | |
2476 | && TREE_CONSTANT (TREE_OPERAND (intop, 1)) | |
2477 | && TREE_CONSTANT (size_exp) | |
2478 | /* If the constant comes from pointer subtraction, | |
2479 | skip this optimization--it would cause an error. */ | |
2480 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE | |
2481 | /* If the constant is unsigned, and smaller than the pointer size, | |
2482 | then we must skip this optimization. This is because it could cause | |
2483 | an overflow error if the constant is negative but INTOP is not. */ | |
2484 | && (! TREE_UNSIGNED (TREE_TYPE (intop)) | |
2485 | || (TYPE_PRECISION (TREE_TYPE (intop)) | |
2486 | == TYPE_PRECISION (TREE_TYPE (ptrop))))) | |
2487 | { | |
2488 | enum tree_code subcode = resultcode; | |
2489 | tree int_type = TREE_TYPE (intop); | |
2490 | if (TREE_CODE (intop) == MINUS_EXPR) | |
2491 | subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); | |
2492 | /* Convert both subexpression types to the type of intop, | |
2493 | because weird cases involving pointer arithmetic | |
2494 | can result in a sum or difference with different type args. */ | |
2495 | ptrop = build_binary_op (subcode, ptrop, | |
2496 | convert (int_type, TREE_OPERAND (intop, 1)), 1); | |
2497 | intop = convert (int_type, TREE_OPERAND (intop, 0)); | |
2498 | } | |
2499 | ||
2500 | /* Convert the integer argument to a type the same size as sizetype | |
2501 | so the multiply won't overflow spuriously. */ | |
2502 | ||
2503 | if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) | |
2504 | || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype)) | |
1cae46be | 2505 | intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype), |
771d21fa | 2506 | TREE_UNSIGNED (sizetype)), intop); |
1c26100f | 2507 | |
2508 | /* Replace the integer argument with a suitable product by the object size. | |
2509 | Do this multiplication as signed, then convert to the appropriate | |
2510 | pointer type (actually unsigned integral). */ | |
2511 | ||
2512 | intop = convert (result_type, | |
2513 | build_binary_op (MULT_EXPR, intop, | |
2514 | convert (TREE_TYPE (intop), size_exp), 1)); | |
2515 | ||
2516 | /* Create the sum or difference. */ | |
2517 | ||
2518 | result = build (resultcode, result_type, ptrop, intop); | |
2519 | ||
2520 | folded = fold (result); | |
2521 | if (folded == result) | |
2522 | TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop); | |
2523 | return folded; | |
2524 | } | |
2525 | \f | |
b0fc3e72 | 2526 | /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, |
2527 | or validate its data type for an `if' or `while' statement or ?..: exp. | |
2528 | ||
2529 | This preparation consists of taking the ordinary | |
2530 | representation of an expression expr and producing a valid tree | |
2531 | boolean expression describing whether expr is nonzero. We could | |
3c2239cf | 2532 | simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1), |
b0fc3e72 | 2533 | but we optimize comparisons, &&, ||, and !. |
2534 | ||
3c2239cf | 2535 | The resulting type should always be `truthvalue_type_node'. */ |
b0fc3e72 | 2536 | |
2537 | tree | |
1cae46be | 2538 | c_common_truthvalue_conversion (tree expr) |
b0fc3e72 | 2539 | { |
baa1f4f5 | 2540 | if (TREE_CODE (expr) == ERROR_MARK) |
2541 | return expr; | |
2542 | ||
a70adbe5 | 2543 | #if 0 /* This appears to be wrong for C++. */ |
baa1f4f5 | 2544 | /* These really should return error_mark_node after 2.4 is stable. |
2545 | But not all callers handle ERROR_MARK properly. */ | |
2546 | switch (TREE_CODE (TREE_TYPE (expr))) | |
2547 | { | |
2548 | case RECORD_TYPE: | |
2549 | error ("struct type value used where scalar is required"); | |
3c2239cf | 2550 | return truthvalue_false_node; |
baa1f4f5 | 2551 | |
2552 | case UNION_TYPE: | |
2553 | error ("union type value used where scalar is required"); | |
3c2239cf | 2554 | return truthvalue_false_node; |
baa1f4f5 | 2555 | |
2556 | case ARRAY_TYPE: | |
2557 | error ("array type value used where scalar is required"); | |
3c2239cf | 2558 | return truthvalue_false_node; |
baa1f4f5 | 2559 | |
2560 | default: | |
2561 | break; | |
2562 | } | |
a70adbe5 | 2563 | #endif /* 0 */ |
baa1f4f5 | 2564 | |
b0fc3e72 | 2565 | switch (TREE_CODE (expr)) |
2566 | { | |
b0fc3e72 | 2567 | case EQ_EXPR: |
b0fc3e72 | 2568 | case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: |
2569 | case TRUTH_ANDIF_EXPR: | |
2570 | case TRUTH_ORIF_EXPR: | |
2571 | case TRUTH_AND_EXPR: | |
2572 | case TRUTH_OR_EXPR: | |
31f6e93c | 2573 | case TRUTH_XOR_EXPR: |
7bbc42b5 | 2574 | case TRUTH_NOT_EXPR: |
3c2239cf | 2575 | TREE_TYPE (expr) = truthvalue_type_node; |
8a7e5d0d | 2576 | return expr; |
3e851b85 | 2577 | |
b0fc3e72 | 2578 | case ERROR_MARK: |
2579 | return expr; | |
2580 | ||
2581 | case INTEGER_CST: | |
3c2239cf | 2582 | return integer_zerop (expr) ? truthvalue_false_node : truthvalue_true_node; |
b0fc3e72 | 2583 | |
2584 | case REAL_CST: | |
3c2239cf | 2585 | return real_zerop (expr) ? truthvalue_false_node : truthvalue_true_node; |
b0fc3e72 | 2586 | |
2587 | case ADDR_EXPR: | |
20dd417a | 2588 | /* If we are taking the address of an external decl, it might be zero |
65b5e6a6 | 2589 | if it is weak, so we cannot optimize. */ |
9308e976 | 2590 | if (DECL_P (TREE_OPERAND (expr, 0)) |
65b5e6a6 | 2591 | && DECL_EXTERNAL (TREE_OPERAND (expr, 0))) |
2592 | break; | |
2593 | ||
b0fc3e72 | 2594 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0))) |
3c2239cf | 2595 | return build (COMPOUND_EXPR, truthvalue_type_node, |
2596 | TREE_OPERAND (expr, 0), truthvalue_true_node); | |
b0fc3e72 | 2597 | else |
3c2239cf | 2598 | return truthvalue_true_node; |
b0fc3e72 | 2599 | |
2203bd5c | 2600 | case COMPLEX_EXPR: |
2ba726d2 | 2601 | return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) |
95809de4 | 2602 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
aff9e656 | 2603 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)), |
2604 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)), | |
2203bd5c | 2605 | 0); |
2606 | ||
b0fc3e72 | 2607 | case NEGATE_EXPR: |
2608 | case ABS_EXPR: | |
2609 | case FLOAT_EXPR: | |
2610 | case FFS_EXPR: | |
6a08d0ab | 2611 | case POPCOUNT_EXPR: |
d10cfa8d | 2612 | /* These don't change whether an object is nonzero or zero. */ |
aff9e656 | 2613 | return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)); |
b0fc3e72 | 2614 | |
2615 | case LROTATE_EXPR: | |
2616 | case RROTATE_EXPR: | |
d10cfa8d | 2617 | /* These don't change whether an object is zero or nonzero, but |
b0fc3e72 | 2618 | we can't ignore them if their second arg has side-effects. */ |
2619 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) | |
3c2239cf | 2620 | return build (COMPOUND_EXPR, truthvalue_type_node, TREE_OPERAND (expr, 1), |
aff9e656 | 2621 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 0))); |
b0fc3e72 | 2622 | else |
aff9e656 | 2623 | return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)); |
73be5127 | 2624 | |
b0fc3e72 | 2625 | case COND_EXPR: |
2626 | /* Distribute the conversion into the arms of a COND_EXPR. */ | |
3c2239cf | 2627 | return fold (build (COND_EXPR, truthvalue_type_node, TREE_OPERAND (expr, 0), |
aff9e656 | 2628 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)), |
2629 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 2)))); | |
b0fc3e72 | 2630 | |
2631 | case CONVERT_EXPR: | |
2632 | /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, | |
2633 | since that affects how `default_conversion' will behave. */ | |
2634 | if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE | |
2635 | || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE) | |
2636 | break; | |
b4b174c3 | 2637 | /* Fall through.... */ |
b0fc3e72 | 2638 | case NOP_EXPR: |
2639 | /* If this is widening the argument, we can ignore it. */ | |
2640 | if (TYPE_PRECISION (TREE_TYPE (expr)) | |
2641 | >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0)))) | |
aff9e656 | 2642 | return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)); |
b0fc3e72 | 2643 | break; |
2644 | ||
b0fc3e72 | 2645 | case MINUS_EXPR: |
920d0fb5 | 2646 | /* Perhaps reduce (x - y) != 0 to (x != y). The expressions |
2647 | aren't guaranteed to the be same for modes that can represent | |
2648 | infinity, since if x and y are both +infinity, or both | |
2649 | -infinity, then x - y is not a number. | |
2650 | ||
2651 | Note that this transformation is safe when x or y is NaN. | |
2652 | (x - y) is then NaN, and both (x - y) != 0 and x != y will | |
2653 | be false. */ | |
2654 | if (HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))) | |
fe0a0255 | 2655 | break; |
b4b174c3 | 2656 | /* Fall through.... */ |
fe0a0255 | 2657 | case BIT_XOR_EXPR: |
a70adbe5 | 2658 | /* This and MINUS_EXPR can be changed into a comparison of the |
fe0a0255 | 2659 | two objects. */ |
b0fc3e72 | 2660 | if (TREE_TYPE (TREE_OPERAND (expr, 0)) |
2661 | == TREE_TYPE (TREE_OPERAND (expr, 1))) | |
2662 | return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0), | |
2663 | TREE_OPERAND (expr, 1), 1); | |
2664 | return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0), | |
2665 | fold (build1 (NOP_EXPR, | |
2666 | TREE_TYPE (TREE_OPERAND (expr, 0)), | |
2667 | TREE_OPERAND (expr, 1))), 1); | |
16837b18 | 2668 | |
c2edbca4 | 2669 | case BIT_AND_EXPR: |
cc4c7eaf | 2670 | if (integer_onep (TREE_OPERAND (expr, 1)) |
3c2239cf | 2671 | && TREE_TYPE (expr) != truthvalue_type_node) |
cc4c7eaf | 2672 | /* Using convert here would cause infinite recursion. */ |
3c2239cf | 2673 | return build1 (NOP_EXPR, truthvalue_type_node, expr); |
cc4c7eaf | 2674 | break; |
c2edbca4 | 2675 | |
16837b18 | 2676 | case MODIFY_EXPR: |
2677 | if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR) | |
2678 | warning ("suggest parentheses around assignment used as truth value"); | |
2679 | break; | |
73be5127 | 2680 | |
31f820d2 | 2681 | default: |
2682 | break; | |
b0fc3e72 | 2683 | } |
2684 | ||
2ba726d2 | 2685 | if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) |
a0748b7d | 2686 | { |
aff9e656 | 2687 | tree t = save_expr (expr); |
a0748b7d | 2688 | return (build_binary_op |
2689 | ((TREE_SIDE_EFFECTS (expr) | |
2690 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), | |
aff9e656 | 2691 | c_common_truthvalue_conversion (build_unary_op (REALPART_EXPR, t, 0)), |
2692 | c_common_truthvalue_conversion (build_unary_op (IMAGPART_EXPR, t, 0)), | |
a0748b7d | 2693 | 0)); |
2694 | } | |
2ba726d2 | 2695 | |
b0fc3e72 | 2696 | return build_binary_op (NE_EXPR, expr, integer_zero_node, 1); |
2697 | } | |
2698 | \f | |
1cae46be | 2699 | static tree builtin_function_2 (const char *, const char *, tree, tree, |
2700 | int, enum built_in_class, int, int, | |
2701 | tree); | |
0d4238dc | 2702 | |
ceee5ef4 | 2703 | /* Make a variant type in the proper way for C/C++, propagating qualifiers |
2704 | down to the element type of an array. */ | |
2705 | ||
2706 | tree | |
1cae46be | 2707 | c_build_qualified_type (tree type, int type_quals) |
ceee5ef4 | 2708 | { |
a5b1863e | 2709 | /* A restrict-qualified pointer type must be a pointer to object or |
2710 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
2711 | REFERENCE_TYPEs, which is appropriate for C++. Unfortunately, | |
2712 | the C++ front-end also use POINTER_TYPE for pointer-to-member | |
2713 | values, so even though it should be illegal to use `restrict' | |
2714 | with such an entity we don't flag that here. Thus, special case | |
2715 | code for that case is required in the C++ front-end. */ | |
2716 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
2717 | && (!POINTER_TYPE_P (type) | |
2718 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
2719 | { | |
2720 | error ("invalid use of `restrict'"); | |
2721 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
2722 | } | |
2723 | ||
ceee5ef4 | 2724 | if (TREE_CODE (type) == ARRAY_TYPE) |
a5b1863e | 2725 | return build_array_type (c_build_qualified_type (TREE_TYPE (type), |
2726 | type_quals), | |
754d8048 | 2727 | TYPE_DOMAIN (type)); |
a5b1863e | 2728 | return build_qualified_type (type, type_quals); |
2729 | } | |
2730 | ||
2731 | /* Apply the TYPE_QUALS to the new DECL. */ | |
2732 | ||
2733 | void | |
1cae46be | 2734 | c_apply_type_quals_to_decl (int type_quals, tree decl) |
a5b1863e | 2735 | { |
73bfa249 | 2736 | if ((type_quals & TYPE_QUAL_CONST) |
1cae46be | 2737 | || (TREE_TYPE (decl) |
73bfa249 | 2738 | && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)) |
a5b1863e | 2739 | TREE_READONLY (decl) = 1; |
2740 | if (type_quals & TYPE_QUAL_VOLATILE) | |
2741 | { | |
2742 | TREE_SIDE_EFFECTS (decl) = 1; | |
2743 | TREE_THIS_VOLATILE (decl) = 1; | |
2744 | } | |
d91a20bc | 2745 | if (type_quals & TYPE_QUAL_RESTRICT) |
a5b1863e | 2746 | { |
d91a20bc | 2747 | if (!TREE_TYPE (decl) |
2748 | || !POINTER_TYPE_P (TREE_TYPE (decl)) | |
2749 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl)))) | |
2750 | error ("invalid use of `restrict'"); | |
2751 | else if (flag_strict_aliasing) | |
ed83aafb | 2752 | /* Indicate we need to make a unique alias set for this pointer. |
2753 | We can't do it here because it might be pointing to an | |
2754 | incomplete type. */ | |
2755 | DECL_POINTER_ALIAS_SET (decl) = -2; | |
a5b1863e | 2756 | } |
2757 | } | |
2758 | ||
b5ba9f3a | 2759 | /* Return the typed-based alias set for T, which may be an expression |
f7c44134 | 2760 | or a type. Return -1 if we don't do anything special. */ |
b5ba9f3a | 2761 | |
f7c44134 | 2762 | HOST_WIDE_INT |
1cae46be | 2763 | c_common_get_alias_set (tree t) |
b5ba9f3a | 2764 | { |
be4f2de7 | 2765 | tree u; |
1cae46be | 2766 | |
be4f2de7 | 2767 | /* Permit type-punning when accessing a union, provided the access |
2768 | is directly through the union. For example, this code does not | |
2769 | permit taking the address of a union member and then storing | |
2770 | through it. Even the type-punning allowed here is a GCC | |
2771 | extension, albeit a common and useful one; the C standard says | |
2772 | that such accesses have implementation-defined behavior. */ | |
2773 | for (u = t; | |
2774 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
2775 | u = TREE_OPERAND (u, 0)) | |
2776 | if (TREE_CODE (u) == COMPONENT_REF | |
2777 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
2778 | return 0; | |
1e2513d9 | 2779 | |
9fcc3e54 | 2780 | /* That's all the expressions we handle specially. */ |
2781 | if (! TYPE_P (t)) | |
2782 | return -1; | |
2783 | ||
d716ce75 | 2784 | /* The C standard guarantees that any object may be accessed via an |
9fcc3e54 | 2785 | lvalue that has character type. */ |
2786 | if (t == char_type_node | |
2787 | || t == signed_char_type_node | |
2788 | || t == unsigned_char_type_node) | |
f7c44134 | 2789 | return 0; |
a5b1863e | 2790 | |
cb59f969 | 2791 | /* If it has the may_alias attribute, it can alias anything. */ |
9fcc3e54 | 2792 | if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t))) |
cb59f969 | 2793 | return 0; |
2794 | ||
1607663f | 2795 | /* The C standard specifically allows aliasing between signed and |
2796 | unsigned variants of the same type. We treat the signed | |
2797 | variant as canonical. */ | |
2798 | if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t)) | |
a8868e19 | 2799 | { |
4070745f | 2800 | tree t1 = c_common_signed_type (t); |
1607663f | 2801 | |
a8868e19 | 2802 | /* t1 == t can happen for boolean nodes which are always unsigned. */ |
2803 | if (t1 != t) | |
2804 | return get_alias_set (t1); | |
2805 | } | |
f7c44134 | 2806 | else if (POINTER_TYPE_P (t)) |
87d2d17e | 2807 | { |
f7c44134 | 2808 | tree t1; |
87d2d17e | 2809 | |
2810 | /* Unfortunately, there is no canonical form of a pointer type. | |
2811 | In particular, if we have `typedef int I', then `int *', and | |
2812 | `I *' are different types. So, we have to pick a canonical | |
2813 | representative. We do this below. | |
44e9fa65 | 2814 | |
40bdc593 | 2815 | Technically, this approach is actually more conservative that |
2816 | it needs to be. In particular, `const int *' and `int *' | |
734c98be | 2817 | should be in different alias sets, according to the C and C++ |
40bdc593 | 2818 | standard, since their types are not the same, and so, |
2819 | technically, an `int **' and `const int **' cannot point at | |
2820 | the same thing. | |
2821 | ||
2822 | But, the standard is wrong. In particular, this code is | |
2823 | legal C++: | |
2824 | ||
2825 | int *ip; | |
2826 | int **ipp = &ip; | |
8d564a28 | 2827 | const int* const* cipp = &ipp; |
40bdc593 | 2828 | |
2829 | And, it doesn't make sense for that to be legal unless you | |
2830 | can dereference IPP and CIPP. So, we ignore cv-qualifiers on | |
2831 | the pointed-to types. This issue has been reported to the | |
2832 | C++ committee. */ | |
211f3116 | 2833 | t1 = build_type_no_quals (t); |
f7c44134 | 2834 | if (t1 != t) |
2835 | return get_alias_set (t1); | |
87d2d17e | 2836 | } |
1e2513d9 | 2837 | |
f7c44134 | 2838 | return -1; |
b5ba9f3a | 2839 | } |
902b4e01 | 2840 | \f |
d4c4d95c | 2841 | /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where the |
d3a4d008 | 2842 | second parameter indicates which OPERATOR is being applied. The COMPLAIN |
2843 | flag controls whether we should diagnose possibly ill-formed | |
2844 | constructs or not. */ | |
902b4e01 | 2845 | tree |
1cae46be | 2846 | c_sizeof_or_alignof_type (tree type, enum tree_code op, int complain) |
902b4e01 | 2847 | { |
d4c4d95c | 2848 | const char *op_name; |
2849 | tree value = NULL; | |
2850 | enum tree_code type_code = TREE_CODE (type); | |
1cae46be | 2851 | |
d4c4d95c | 2852 | my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720); |
2853 | op_name = op == SIZEOF_EXPR ? "sizeof" : "__alignof__"; | |
1cae46be | 2854 | |
d4c4d95c | 2855 | if (type_code == FUNCTION_TYPE) |
902b4e01 | 2856 | { |
d4c4d95c | 2857 | if (op == SIZEOF_EXPR) |
2858 | { | |
d3a4d008 | 2859 | if (complain && (pedantic || warn_pointer_arith)) |
d4c4d95c | 2860 | pedwarn ("invalid application of `sizeof' to a function type"); |
2861 | value = size_one_node; | |
2862 | } | |
2863 | else | |
2864 | value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); | |
2865 | } | |
2866 | else if (type_code == VOID_TYPE || type_code == ERROR_MARK) | |
2867 | { | |
1cae46be | 2868 | if (type_code == VOID_TYPE |
d3a4d008 | 2869 | && complain && (pedantic || warn_pointer_arith)) |
d4c4d95c | 2870 | pedwarn ("invalid application of `%s' to a void type", op_name); |
2871 | value = size_one_node; | |
902b4e01 | 2872 | } |
902b4e01 | 2873 | else if (!COMPLETE_TYPE_P (type)) |
2874 | { | |
d3a4d008 | 2875 | if (complain) |
2876 | error ("invalid application of `%s' to an incomplete type", op_name); | |
d4c4d95c | 2877 | value = size_zero_node; |
902b4e01 | 2878 | } |
2879 | else | |
d4c4d95c | 2880 | { |
2881 | if (op == SIZEOF_EXPR) | |
2882 | /* Convert in case a char is more than one unit. */ | |
2883 | value = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), | |
2884 | size_int (TYPE_PRECISION (char_type_node) | |
2885 | / BITS_PER_UNIT)); | |
2886 | else | |
2887 | value = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT); | |
2888 | } | |
902b4e01 | 2889 | |
d4c4d95c | 2890 | /* VALUE will have an integer type with TYPE_IS_SIZETYPE set. |
2891 | TYPE_IS_SIZETYPE means that certain things (like overflow) will | |
2892 | never happen. However, this node should really have type | |
2893 | `size_t', which is just a typedef for an ordinary integer type. */ | |
654ef926 | 2894 | value = fold (build1 (NOP_EXPR, size_type_node, value)); |
d4c4d95c | 2895 | my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)), 20001021); |
1cae46be | 2896 | |
d4c4d95c | 2897 | return value; |
902b4e01 | 2898 | } |
2899 | ||
2900 | /* Implement the __alignof keyword: Return the minimum required | |
2901 | alignment of EXPR, measured in bytes. For VAR_DECL's and | |
2902 | FIELD_DECL's return DECL_ALIGN (which can be set from an | |
2903 | "aligned" __attribute__ specification). */ | |
72040e7e | 2904 | |
902b4e01 | 2905 | tree |
1cae46be | 2906 | c_alignof_expr (tree expr) |
902b4e01 | 2907 | { |
2908 | tree t; | |
2909 | ||
2910 | if (TREE_CODE (expr) == VAR_DECL) | |
2911 | t = size_int (DECL_ALIGN (expr) / BITS_PER_UNIT); | |
1cae46be | 2912 | |
902b4e01 | 2913 | else if (TREE_CODE (expr) == COMPONENT_REF |
2914 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
2915 | { | |
2916 | error ("`__alignof' applied to a bit-field"); | |
2917 | t = size_one_node; | |
2918 | } | |
2919 | else if (TREE_CODE (expr) == COMPONENT_REF | |
7cc7e163 | 2920 | && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) |
902b4e01 | 2921 | t = size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT); |
1cae46be | 2922 | |
902b4e01 | 2923 | else if (TREE_CODE (expr) == INDIRECT_REF) |
2924 | { | |
2925 | tree t = TREE_OPERAND (expr, 0); | |
2926 | tree best = t; | |
2927 | int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
1cae46be | 2928 | |
902b4e01 | 2929 | while (TREE_CODE (t) == NOP_EXPR |
7cc7e163 | 2930 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) |
902b4e01 | 2931 | { |
2932 | int thisalign; | |
2933 | ||
2934 | t = TREE_OPERAND (t, 0); | |
2935 | thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
2936 | if (thisalign > bestalign) | |
2937 | best = t, bestalign = thisalign; | |
2938 | } | |
2939 | return c_alignof (TREE_TYPE (TREE_TYPE (best))); | |
2940 | } | |
2941 | else | |
2942 | return c_alignof (TREE_TYPE (expr)); | |
2943 | ||
654ef926 | 2944 | return fold (build1 (NOP_EXPR, size_type_node, t)); |
902b4e01 | 2945 | } |
2946 | \f | |
8fe4a266 | 2947 | /* Handle C and C++ default attributes. */ |
2948 | ||
2949 | enum built_in_attribute | |
2950 | { | |
2951 | #define DEF_ATTR_NULL_TREE(ENUM) ENUM, | |
2952 | #define DEF_ATTR_INT(ENUM, VALUE) ENUM, | |
2953 | #define DEF_ATTR_IDENT(ENUM, STRING) ENUM, | |
2954 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM, | |
8fe4a266 | 2955 | #include "builtin-attrs.def" |
2956 | #undef DEF_ATTR_NULL_TREE | |
2957 | #undef DEF_ATTR_INT | |
2958 | #undef DEF_ATTR_IDENT | |
2959 | #undef DEF_ATTR_TREE_LIST | |
8fe4a266 | 2960 | ATTR_LAST |
2961 | }; | |
2962 | ||
2963 | static GTY(()) tree built_in_attributes[(int) ATTR_LAST]; | |
2964 | ||
1cae46be | 2965 | static void c_init_attributes (void); |
8fe4a266 | 2966 | |
72040e7e | 2967 | /* Build tree nodes and builtin functions common to both C and C++ language |
0270ae90 | 2968 | frontends. */ |
f7c44134 | 2969 | |
72040e7e | 2970 | void |
1cae46be | 2971 | c_common_nodes_and_builtins (void) |
72040e7e | 2972 | { |
1cae46be | 2973 | enum builtin_type |
d2d4bdde | 2974 | { |
2975 | #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME, | |
2976 | #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME, | |
2977 | #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME, | |
2978 | #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME, | |
2979 | #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, | |
2980 | #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, | |
2981 | #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME, | |
2982 | #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME, | |
2983 | #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME, | |
03901330 | 2984 | #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, |
d2d4bdde | 2985 | #define DEF_POINTER_TYPE(NAME, TYPE) NAME, |
2986 | #include "builtin-types.def" | |
2987 | #undef DEF_PRIMITIVE_TYPE | |
2988 | #undef DEF_FUNCTION_TYPE_0 | |
2989 | #undef DEF_FUNCTION_TYPE_1 | |
2990 | #undef DEF_FUNCTION_TYPE_2 | |
2991 | #undef DEF_FUNCTION_TYPE_3 | |
2992 | #undef DEF_FUNCTION_TYPE_4 | |
2993 | #undef DEF_FUNCTION_TYPE_VAR_0 | |
2994 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
2995 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
03901330 | 2996 | #undef DEF_FUNCTION_TYPE_VAR_3 |
d2d4bdde | 2997 | #undef DEF_POINTER_TYPE |
2998 | BT_LAST | |
2999 | }; | |
3000 | ||
3001 | typedef enum builtin_type builtin_type; | |
3002 | ||
7cc7e163 | 3003 | tree builtin_types[(int) BT_LAST]; |
174fcc61 | 3004 | int wchar_type_size; |
3005 | tree array_domain_type; | |
2d47cc32 | 3006 | tree va_list_ref_type_node; |
8a15c04a | 3007 | tree va_list_arg_type_node; |
a66c9326 | 3008 | |
174fcc61 | 3009 | /* Define `int' and `char' first so that dbx will output them first. */ |
d946ea19 | 3010 | record_builtin_type (RID_INT, NULL, integer_type_node); |
174fcc61 | 3011 | record_builtin_type (RID_CHAR, "char", char_type_node); |
3012 | ||
3013 | /* `signed' is the same as `int'. FIXME: the declarations of "signed", | |
3014 | "unsigned long", "long long unsigned" and "unsigned short" were in C++ | |
3015 | but not C. Are the conditionals here needed? */ | |
c0f19401 | 3016 | if (c_dialect_cxx ()) |
d946ea19 | 3017 | record_builtin_type (RID_SIGNED, NULL, integer_type_node); |
174fcc61 | 3018 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); |
3019 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); | |
3020 | record_builtin_type (RID_MAX, "long unsigned int", | |
3021 | long_unsigned_type_node); | |
c0f19401 | 3022 | if (c_dialect_cxx ()) |
174fcc61 | 3023 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); |
3024 | record_builtin_type (RID_MAX, "long long int", | |
3025 | long_long_integer_type_node); | |
3026 | record_builtin_type (RID_MAX, "long long unsigned int", | |
3027 | long_long_unsigned_type_node); | |
c0f19401 | 3028 | if (c_dialect_cxx ()) |
174fcc61 | 3029 | record_builtin_type (RID_MAX, "long long unsigned", |
3030 | long_long_unsigned_type_node); | |
3031 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); | |
3032 | record_builtin_type (RID_MAX, "short unsigned int", | |
3033 | short_unsigned_type_node); | |
c0f19401 | 3034 | if (c_dialect_cxx ()) |
174fcc61 | 3035 | record_builtin_type (RID_MAX, "unsigned short", |
3036 | short_unsigned_type_node); | |
3037 | ||
3038 | /* Define both `signed char' and `unsigned char'. */ | |
3039 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); | |
3040 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); | |
3041 | ||
771d21fa | 3042 | /* These are types that c_common_type_for_size and |
3043 | c_common_type_for_mode use. */ | |
20325f61 | 3044 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE, |
3045 | intQI_type_node)); | |
3046 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE, | |
3047 | intHI_type_node)); | |
3048 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE, | |
3049 | intSI_type_node)); | |
3050 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE, | |
3051 | intDI_type_node)); | |
174fcc61 | 3052 | #if HOST_BITS_PER_WIDE_INT >= 64 |
20325f61 | 3053 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, |
3054 | get_identifier ("__int128_t"), | |
3055 | intTI_type_node)); | |
174fcc61 | 3056 | #endif |
20325f61 | 3057 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE, |
3058 | unsigned_intQI_type_node)); | |
3059 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE, | |
3060 | unsigned_intHI_type_node)); | |
3061 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE, | |
3062 | unsigned_intSI_type_node)); | |
3063 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE, | |
3064 | unsigned_intDI_type_node)); | |
174fcc61 | 3065 | #if HOST_BITS_PER_WIDE_INT >= 64 |
20325f61 | 3066 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, |
3067 | get_identifier ("__uint128_t"), | |
3068 | unsigned_intTI_type_node)); | |
174fcc61 | 3069 | #endif |
3070 | ||
3071 | /* Create the widest literal types. */ | |
3072 | widest_integer_literal_type_node | |
3073 | = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
20325f61 | 3074 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE, |
3075 | widest_integer_literal_type_node)); | |
174fcc61 | 3076 | |
3077 | widest_unsigned_literal_type_node | |
3078 | = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
20325f61 | 3079 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE, |
3080 | widest_unsigned_literal_type_node)); | |
174fcc61 | 3081 | |
3082 | /* `unsigned long' is the standard type for sizeof. | |
3083 | Note that stddef.h uses `unsigned long', | |
3084 | and this must agree, even if long and int are the same size. */ | |
654ef926 | 3085 | size_type_node = |
174fcc61 | 3086 | TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE))); |
654ef926 | 3087 | signed_size_type_node = c_common_signed_type (size_type_node); |
3088 | set_sizetype (size_type_node); | |
174fcc61 | 3089 | |
3090 | build_common_tree_nodes_2 (flag_short_double); | |
3091 | ||
d946ea19 | 3092 | record_builtin_type (RID_FLOAT, NULL, float_type_node); |
3093 | record_builtin_type (RID_DOUBLE, NULL, double_type_node); | |
174fcc61 | 3094 | record_builtin_type (RID_MAX, "long double", long_double_type_node); |
3095 | ||
20325f61 | 3096 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, |
3097 | get_identifier ("complex int"), | |
3098 | complex_integer_type_node)); | |
3099 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, | |
3100 | get_identifier ("complex float"), | |
3101 | complex_float_type_node)); | |
3102 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, | |
3103 | get_identifier ("complex double"), | |
3104 | complex_double_type_node)); | |
3105 | (*lang_hooks.decls.pushdecl) | |
3106 | (build_decl (TYPE_DECL, get_identifier ("complex long double"), | |
3107 | complex_long_double_type_node)); | |
174fcc61 | 3108 | |
175f3f99 | 3109 | /* Types which are common to the fortran compiler and libf2c. When |
3110 | changing these, you also need to be concerned with f/com.h. */ | |
3111 | ||
3112 | if (TYPE_PRECISION (float_type_node) | |
3113 | == TYPE_PRECISION (long_integer_type_node)) | |
3114 | { | |
3115 | g77_integer_type_node = long_integer_type_node; | |
3116 | g77_uinteger_type_node = long_unsigned_type_node; | |
3117 | } | |
3118 | else if (TYPE_PRECISION (float_type_node) | |
3119 | == TYPE_PRECISION (integer_type_node)) | |
3120 | { | |
3121 | g77_integer_type_node = integer_type_node; | |
3122 | g77_uinteger_type_node = unsigned_type_node; | |
3123 | } | |
3124 | else | |
3125 | g77_integer_type_node = g77_uinteger_type_node = NULL_TREE; | |
3126 | ||
3127 | if (g77_integer_type_node != NULL_TREE) | |
3128 | { | |
3129 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, | |
3130 | get_identifier ("__g77_integer"), | |
3131 | g77_integer_type_node)); | |
3132 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, | |
3133 | get_identifier ("__g77_uinteger"), | |
3134 | g77_uinteger_type_node)); | |
3135 | } | |
3136 | ||
3137 | if (TYPE_PRECISION (float_type_node) * 2 | |
3138 | == TYPE_PRECISION (long_integer_type_node)) | |
3139 | { | |
3140 | g77_longint_type_node = long_integer_type_node; | |
3141 | g77_ulongint_type_node = long_unsigned_type_node; | |
3142 | } | |
3143 | else if (TYPE_PRECISION (float_type_node) * 2 | |
3144 | == TYPE_PRECISION (long_long_integer_type_node)) | |
3145 | { | |
3146 | g77_longint_type_node = long_long_integer_type_node; | |
3147 | g77_ulongint_type_node = long_long_unsigned_type_node; | |
3148 | } | |
3149 | else | |
3150 | g77_longint_type_node = g77_ulongint_type_node = NULL_TREE; | |
3151 | ||
3152 | if (g77_longint_type_node != NULL_TREE) | |
3153 | { | |
3154 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, | |
3155 | get_identifier ("__g77_longint"), | |
3156 | g77_longint_type_node)); | |
3157 | (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, | |
3158 | get_identifier ("__g77_ulongint"), | |
3159 | g77_ulongint_type_node)); | |
3160 | } | |
3161 | ||
d946ea19 | 3162 | record_builtin_type (RID_VOID, NULL, void_type_node); |
174fcc61 | 3163 | |
d2d4bdde | 3164 | void_zero_node = build_int_2 (0, 0); |
3165 | TREE_TYPE (void_zero_node) = void_type_node; | |
3166 | ||
174fcc61 | 3167 | void_list_node = build_void_list_node (); |
3168 | ||
3169 | /* Make a type to be the domain of a few array types | |
3170 | whose domains don't really matter. | |
3171 | 200 is small enough that it always fits in size_t | |
3172 | and large enough that it can hold most function names for the | |
3173 | initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
3174 | array_domain_type = build_index_type (size_int (200)); | |
3175 | ||
3176 | /* Make a type for arrays of characters. | |
3177 | With luck nothing will ever really depend on the length of this | |
3178 | array type. */ | |
3179 | char_array_type_node | |
3180 | = build_array_type (char_type_node, array_domain_type); | |
3181 | ||
3182 | /* Likewise for arrays of ints. */ | |
3183 | int_array_type_node | |
3184 | = build_array_type (integer_type_node, array_domain_type); | |
3185 | ||
d2d4bdde | 3186 | string_type_node = build_pointer_type (char_type_node); |
3187 | const_string_type_node | |
3188 | = build_pointer_type (build_qualified_type | |
3189 | (char_type_node, TYPE_QUAL_CONST)); | |
3190 | ||
174fcc61 | 3191 | /* This is special for C++ so functions can be overloaded. */ |
18ef7ac2 | 3192 | wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE); |
174fcc61 | 3193 | wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); |
3194 | wchar_type_size = TYPE_PRECISION (wchar_type_node); | |
c0f19401 | 3195 | if (c_dialect_cxx ()) |
174fcc61 | 3196 | { |
3197 | if (TREE_UNSIGNED (wchar_type_node)) | |
3198 | wchar_type_node = make_unsigned_type (wchar_type_size); | |
3199 | else | |
3200 | wchar_type_node = make_signed_type (wchar_type_size); | |
3201 | record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); | |
3202 | } | |
3203 | else | |
3204 | { | |
4070745f | 3205 | signed_wchar_type_node = c_common_signed_type (wchar_type_node); |
3206 | unsigned_wchar_type_node = c_common_unsigned_type (wchar_type_node); | |
174fcc61 | 3207 | } |
3208 | ||
3209 | /* This is for wide string constants. */ | |
3210 | wchar_array_type_node | |
3211 | = build_array_type (wchar_type_node, array_domain_type); | |
3212 | ||
6bf5ed8d | 3213 | wint_type_node = |
3214 | TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); | |
3215 | ||
3216 | intmax_type_node = | |
3217 | TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); | |
3218 | uintmax_type_node = | |
3219 | TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); | |
3220 | ||
3221 | default_function_type = build_function_type (integer_type_node, NULL_TREE); | |
3222 | ptrdiff_type_node | |
3223 | = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); | |
4070745f | 3224 | unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node); |
6bf5ed8d | 3225 | |
20325f61 | 3226 | (*lang_hooks.decls.pushdecl) |
3227 | (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"), | |
3228 | va_list_type_node)); | |
8a15c04a | 3229 | |
20325f61 | 3230 | (*lang_hooks.decls.pushdecl) |
3231 | (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"), | |
3232 | ptrdiff_type_node)); | |
ee702940 | 3233 | |
20325f61 | 3234 | (*lang_hooks.decls.pushdecl) |
3235 | (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"), | |
3236 | sizetype)); | |
ee702940 | 3237 | |
8a15c04a | 3238 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
2d47cc32 | 3239 | { |
3240 | va_list_arg_type_node = va_list_ref_type_node = | |
3241 | build_pointer_type (TREE_TYPE (va_list_type_node)); | |
3242 | } | |
8a15c04a | 3243 | else |
2d47cc32 | 3244 | { |
3245 | va_list_arg_type_node = va_list_type_node; | |
3246 | va_list_ref_type_node = build_reference_type (va_list_type_node); | |
3247 | } | |
1cae46be | 3248 | |
d2d4bdde | 3249 | #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ |
3250 | builtin_types[(int) ENUM] = VALUE; | |
3251 | #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ | |
3252 | builtin_types[(int) ENUM] \ | |
3253 | = build_function_type (builtin_types[(int) RETURN], \ | |
3254 | void_list_node); | |
3255 | #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ | |
3256 | builtin_types[(int) ENUM] \ | |
3257 | = build_function_type (builtin_types[(int) RETURN], \ | |
3258 | tree_cons (NULL_TREE, \ | |
3259 | builtin_types[(int) ARG1], \ | |
3260 | void_list_node)); | |
3261 | #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ | |
3262 | builtin_types[(int) ENUM] \ | |
1cae46be | 3263 | = build_function_type \ |
d2d4bdde | 3264 | (builtin_types[(int) RETURN], \ |
3265 | tree_cons (NULL_TREE, \ | |
3266 | builtin_types[(int) ARG1], \ | |
3267 | tree_cons (NULL_TREE, \ | |
3268 | builtin_types[(int) ARG2], \ | |
3269 | void_list_node))); | |
3270 | #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
3271 | builtin_types[(int) ENUM] \ | |
3272 | = build_function_type \ | |
3273 | (builtin_types[(int) RETURN], \ | |
3274 | tree_cons (NULL_TREE, \ | |
3275 | builtin_types[(int) ARG1], \ | |
3276 | tree_cons (NULL_TREE, \ | |
3277 | builtin_types[(int) ARG2], \ | |
3278 | tree_cons (NULL_TREE, \ | |
3279 | builtin_types[(int) ARG3], \ | |
3280 | void_list_node)))); | |
3281 | #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
3282 | builtin_types[(int) ENUM] \ | |
3283 | = build_function_type \ | |
3284 | (builtin_types[(int) RETURN], \ | |
3285 | tree_cons (NULL_TREE, \ | |
3286 | builtin_types[(int) ARG1], \ | |
3287 | tree_cons (NULL_TREE, \ | |
3288 | builtin_types[(int) ARG2], \ | |
1cae46be | 3289 | tree_cons \ |
d2d4bdde | 3290 | (NULL_TREE, \ |
1cae46be | 3291 | builtin_types[(int) ARG3], \ |
d2d4bdde | 3292 | tree_cons (NULL_TREE, \ |
3293 | builtin_types[(int) ARG4], \ | |
3294 | void_list_node))))); | |
3295 | #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ | |
3296 | builtin_types[(int) ENUM] \ | |
3297 | = build_function_type (builtin_types[(int) RETURN], NULL_TREE); | |
3298 | #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ | |
3299 | builtin_types[(int) ENUM] \ | |
1cae46be | 3300 | = build_function_type (builtin_types[(int) RETURN], \ |
d2d4bdde | 3301 | tree_cons (NULL_TREE, \ |
3302 | builtin_types[(int) ARG1], \ | |
3311f67b | 3303 | NULL_TREE)); |
3304 | ||
d2d4bdde | 3305 | #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ |
3306 | builtin_types[(int) ENUM] \ | |
1cae46be | 3307 | = build_function_type \ |
d2d4bdde | 3308 | (builtin_types[(int) RETURN], \ |
3309 | tree_cons (NULL_TREE, \ | |
3310 | builtin_types[(int) ARG1], \ | |
3311 | tree_cons (NULL_TREE, \ | |
3312 | builtin_types[(int) ARG2], \ | |
3313 | NULL_TREE))); | |
03901330 | 3314 | |
3315 | #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
3316 | builtin_types[(int) ENUM] \ | |
1cae46be | 3317 | = build_function_type \ |
03901330 | 3318 | (builtin_types[(int) RETURN], \ |
3319 | tree_cons (NULL_TREE, \ | |
3320 | builtin_types[(int) ARG1], \ | |
3321 | tree_cons (NULL_TREE, \ | |
3322 | builtin_types[(int) ARG2], \ | |
3323 | tree_cons (NULL_TREE, \ | |
3324 | builtin_types[(int) ARG3], \ | |
3325 | NULL_TREE)))); | |
3326 | ||
d2d4bdde | 3327 | #define DEF_POINTER_TYPE(ENUM, TYPE) \ |
3328 | builtin_types[(int) ENUM] \ | |
3329 | = build_pointer_type (builtin_types[(int) TYPE]); | |
3330 | #include "builtin-types.def" | |
3331 | #undef DEF_PRIMITIVE_TYPE | |
3332 | #undef DEF_FUNCTION_TYPE_1 | |
3333 | #undef DEF_FUNCTION_TYPE_2 | |
3334 | #undef DEF_FUNCTION_TYPE_3 | |
3335 | #undef DEF_FUNCTION_TYPE_4 | |
3336 | #undef DEF_FUNCTION_TYPE_VAR_0 | |
3337 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
03901330 | 3338 | #undef DEF_FUNCTION_TYPE_VAR_2 |
3339 | #undef DEF_FUNCTION_TYPE_VAR_3 | |
d2d4bdde | 3340 | #undef DEF_POINTER_TYPE |
3341 | ||
101cc430 | 3342 | c_init_attributes (); |
8fe4a266 | 3343 | |
3344 | #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, \ | |
0a68165a | 3345 | BOTH_P, FALLBACK_P, NONANSI_P, ATTRS, IMPLICIT) \ |
d2d4bdde | 3346 | if (NAME) \ |
3347 | { \ | |
3348 | tree decl; \ | |
3349 | \ | |
3350 | if (strncmp (NAME, "__builtin_", strlen ("__builtin_")) != 0) \ | |
3351 | abort (); \ | |
3352 | \ | |
3353 | if (!BOTH_P) \ | |
3354 | decl = builtin_function (NAME, builtin_types[TYPE], ENUM, \ | |
3355 | CLASS, \ | |
3356 | (FALLBACK_P \ | |
3357 | ? (NAME + strlen ("__builtin_")) \ | |
8fe4a266 | 3358 | : NULL), \ |
3359 | built_in_attributes[(int) ATTRS]); \ | |
d2d4bdde | 3360 | else \ |
3361 | decl = builtin_function_2 (NAME, \ | |
3362 | NAME + strlen ("__builtin_"), \ | |
3363 | builtin_types[TYPE], \ | |
3364 | builtin_types[LIBTYPE], \ | |
3365 | ENUM, \ | |
3366 | CLASS, \ | |
3367 | FALLBACK_P, \ | |
3368 | NONANSI_P, \ | |
8fe4a266 | 3369 | built_in_attributes[(int) ATTRS]); \ |
d2d4bdde | 3370 | \ |
3371 | built_in_decls[(int) ENUM] = decl; \ | |
0a68165a | 3372 | if (IMPLICIT) \ |
3373 | implicit_built_in_decls[(int) ENUM] = decl; \ | |
1cae46be | 3374 | } |
d2d4bdde | 3375 | #include "builtins.def" |
3376 | #undef DEF_BUILTIN | |
df4b504c | 3377 | |
ffa8918b | 3378 | (*targetm.init_builtins) (); |
3379 | ||
5c62f199 | 3380 | main_identifier_node = get_identifier ("main"); |
72040e7e | 3381 | } |
a66c9326 | 3382 | |
3383 | tree | |
1cae46be | 3384 | build_va_arg (tree expr, tree type) |
a66c9326 | 3385 | { |
3386 | return build1 (VA_ARG_EXPR, type, expr); | |
3387 | } | |
0d4238dc | 3388 | |
3389 | ||
dd878098 | 3390 | /* Linked list of disabled built-in functions. */ |
3391 | ||
3392 | typedef struct disabled_builtin | |
3393 | { | |
3394 | const char *name; | |
3395 | struct disabled_builtin *next; | |
3396 | } disabled_builtin; | |
3397 | static disabled_builtin *disabled_builtins = NULL; | |
3398 | ||
1cae46be | 3399 | static bool builtin_function_disabled_p (const char *); |
dd878098 | 3400 | |
3401 | /* Disable a built-in function specified by -fno-builtin-NAME. If NAME | |
3402 | begins with "__builtin_", give an error. */ | |
3403 | ||
3404 | void | |
1cae46be | 3405 | disable_builtin_function (const char *name) |
dd878098 | 3406 | { |
3407 | if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0) | |
3408 | error ("cannot disable built-in function `%s'", name); | |
3409 | else | |
3410 | { | |
3411 | disabled_builtin *new = xmalloc (sizeof (disabled_builtin)); | |
3412 | new->name = name; | |
3413 | new->next = disabled_builtins; | |
3414 | disabled_builtins = new; | |
3415 | } | |
3416 | } | |
3417 | ||
3418 | ||
3419 | /* Return true if the built-in function NAME has been disabled, false | |
3420 | otherwise. */ | |
3421 | ||
3422 | static bool | |
1cae46be | 3423 | builtin_function_disabled_p (const char *name) |
dd878098 | 3424 | { |
3425 | disabled_builtin *p; | |
3426 | for (p = disabled_builtins; p != NULL; p = p->next) | |
3427 | { | |
3428 | if (strcmp (name, p->name) == 0) | |
3429 | return true; | |
3430 | } | |
3431 | return false; | |
3432 | } | |
3433 | ||
3434 | ||
0d4238dc | 3435 | /* Possibly define a builtin function with one or two names. BUILTIN_NAME |
3436 | is an __builtin_-prefixed name; NAME is the ordinary name; one or both | |
3437 | of these may be NULL (though both being NULL is useless). | |
3438 | BUILTIN_TYPE is the type of the __builtin_-prefixed function; | |
3439 | TYPE is the type of the function with the ordinary name. These | |
3440 | may differ if the ordinary name is declared with a looser type to avoid | |
3441 | conflicts with headers. FUNCTION_CODE and CLASS are as for | |
3442 | builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as | |
3443 | the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME. | |
8fe4a266 | 3444 | If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; |
3445 | ATTRS is the tree list representing the builtin's function attributes. | |
0d4238dc | 3446 | Returns the declaration of BUILTIN_NAME, if any, otherwise |
3447 | the declaration of NAME. Does not declare NAME if flag_no_builtin, | |
3448 | or if NONANSI_P and flag_no_nonansi_builtin. */ | |
3449 | ||
3450 | static tree | |
1cae46be | 3451 | builtin_function_2 (const char *builtin_name, const char *name, |
3452 | tree builtin_type, tree type, int function_code, | |
3453 | enum built_in_class class, int library_name_p, | |
3454 | int nonansi_p, tree attrs) | |
0d4238dc | 3455 | { |
3456 | tree bdecl = NULL_TREE; | |
3457 | tree decl = NULL_TREE; | |
a40609e6 | 3458 | |
0d4238dc | 3459 | if (builtin_name != 0) |
a40609e6 | 3460 | bdecl = builtin_function (builtin_name, builtin_type, function_code, |
3461 | class, library_name_p ? name : NULL, attrs); | |
3462 | ||
dd878098 | 3463 | if (name != 0 && !flag_no_builtin && !builtin_function_disabled_p (name) |
3464 | && !(nonansi_p && flag_no_nonansi_builtin)) | |
a40609e6 | 3465 | decl = builtin_function (name, type, function_code, class, NULL, attrs); |
3466 | ||
0d4238dc | 3467 | return (bdecl != 0 ? bdecl : decl); |
3468 | } | |
e94026da | 3469 | \f |
d7aeef06 | 3470 | /* Nonzero if the type T promotes to int. This is (nearly) the |
3471 | integral promotions defined in ISO C99 6.3.1.1/2. */ | |
3472 | ||
3473 | bool | |
1cae46be | 3474 | c_promoting_integer_type_p (tree t) |
d7aeef06 | 3475 | { |
3476 | switch (TREE_CODE (t)) | |
3477 | { | |
3478 | case INTEGER_TYPE: | |
3479 | return (TYPE_MAIN_VARIANT (t) == char_type_node | |
3480 | || TYPE_MAIN_VARIANT (t) == signed_char_type_node | |
3481 | || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node | |
3482 | || TYPE_MAIN_VARIANT (t) == short_integer_type_node | |
7aa1e6eb | 3483 | || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node |
3484 | || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node)); | |
d7aeef06 | 3485 | |
3486 | case ENUMERAL_TYPE: | |
3487 | /* ??? Technically all enumerations not larger than an int | |
3488 | promote to an int. But this is used along code paths | |
3489 | that only want to notice a size change. */ | |
3490 | return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node); | |
3491 | ||
3492 | case BOOLEAN_TYPE: | |
3493 | return 1; | |
3494 | ||
3495 | default: | |
3496 | return 0; | |
3497 | } | |
3498 | } | |
3499 | ||
e94026da | 3500 | /* Return 1 if PARMS specifies a fixed number of parameters |
3501 | and none of their types is affected by default promotions. */ | |
3502 | ||
3503 | int | |
1cae46be | 3504 | self_promoting_args_p (tree parms) |
e94026da | 3505 | { |
19cb6b50 | 3506 | tree t; |
e94026da | 3507 | for (t = parms; t; t = TREE_CHAIN (t)) |
3508 | { | |
19cb6b50 | 3509 | tree type = TREE_VALUE (t); |
43f74bc4 | 3510 | |
e94026da | 3511 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
3512 | return 0; | |
3513 | ||
3514 | if (type == 0) | |
3515 | return 0; | |
3516 | ||
3517 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
3518 | return 0; | |
3519 | ||
d7aeef06 | 3520 | if (c_promoting_integer_type_p (type)) |
e94026da | 3521 | return 0; |
3522 | } | |
3523 | return 1; | |
3524 | } | |
605fb01e | 3525 | |
c25509f2 | 3526 | /* Recursively examines the array elements of TYPE, until a non-array |
3527 | element type is found. */ | |
3528 | ||
3529 | tree | |
1cae46be | 3530 | strip_array_types (tree type) |
c25509f2 | 3531 | { |
3532 | while (TREE_CODE (type) == ARRAY_TYPE) | |
3533 | type = TREE_TYPE (type); | |
3534 | ||
3535 | return type; | |
3536 | } | |
3537 | ||
1cae46be | 3538 | static tree expand_unordered_cmp (tree, tree, enum tree_code, enum tree_code); |
920d0fb5 | 3539 | |
3540 | /* Expand a call to an unordered comparison function such as | |
3541 | __builtin_isgreater(). FUNCTION is the function's declaration and | |
3542 | PARAMS a list of the values passed. For __builtin_isunordered(), | |
3543 | UNORDERED_CODE is UNORDERED_EXPR and ORDERED_CODE is NOP_EXPR. In | |
3544 | other cases, UNORDERED_CODE and ORDERED_CODE are comparison codes | |
3545 | that give the opposite of the desired result. UNORDERED_CODE is | |
3546 | used for modes that can hold NaNs and ORDERED_CODE is used for the | |
3547 | rest. */ | |
3548 | ||
3549 | static tree | |
1cae46be | 3550 | expand_unordered_cmp (tree function, tree params, |
3551 | enum tree_code unordered_code, | |
3552 | enum tree_code ordered_code) | |
920d0fb5 | 3553 | { |
3554 | tree arg0, arg1, type; | |
3555 | enum tree_code code0, code1; | |
3556 | ||
3557 | /* Check that we have exactly two arguments. */ | |
3558 | if (params == 0 || TREE_CHAIN (params) == 0) | |
3559 | { | |
3560 | error ("too few arguments to function `%s'", | |
3561 | IDENTIFIER_POINTER (DECL_NAME (function))); | |
3562 | return error_mark_node; | |
3563 | } | |
3564 | else if (TREE_CHAIN (TREE_CHAIN (params)) != 0) | |
3565 | { | |
3566 | error ("too many arguments to function `%s'", | |
3567 | IDENTIFIER_POINTER (DECL_NAME (function))); | |
3568 | return error_mark_node; | |
3569 | } | |
3570 | ||
3571 | arg0 = TREE_VALUE (params); | |
3572 | arg1 = TREE_VALUE (TREE_CHAIN (params)); | |
3573 | ||
3574 | code0 = TREE_CODE (TREE_TYPE (arg0)); | |
3575 | code1 = TREE_CODE (TREE_TYPE (arg1)); | |
3576 | ||
3577 | /* Make sure that the arguments have a common type of REAL. */ | |
3578 | type = 0; | |
3579 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) | |
3580 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
3581 | type = common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)); | |
3582 | ||
3583 | if (type == 0 || TREE_CODE (type) != REAL_TYPE) | |
3584 | { | |
3585 | error ("non-floating-point argument to function `%s'", | |
3586 | IDENTIFIER_POINTER (DECL_NAME (function))); | |
3587 | return error_mark_node; | |
3588 | } | |
3589 | ||
3590 | if (unordered_code == UNORDERED_EXPR) | |
3591 | { | |
3592 | if (MODE_HAS_NANS (TYPE_MODE (type))) | |
3593 | return build_binary_op (unordered_code, | |
3594 | convert (type, arg0), | |
3595 | convert (type, arg1), | |
3596 | 0); | |
3597 | else | |
3598 | return integer_zero_node; | |
3599 | } | |
3600 | ||
3601 | return build_unary_op (TRUTH_NOT_EXPR, | |
3602 | build_binary_op (MODE_HAS_NANS (TYPE_MODE (type)) | |
3603 | ? unordered_code | |
3604 | : ordered_code, | |
3605 | convert (type, arg0), | |
3606 | convert (type, arg1), | |
3607 | 0), | |
3608 | 0); | |
3609 | } | |
3610 | ||
3611 | ||
605fb01e | 3612 | /* Recognize certain built-in functions so we can make tree-codes |
3613 | other than CALL_EXPR. We do this when it enables fold-const.c | |
3614 | to do something useful. */ | |
3615 | /* ??? By rights this should go in builtins.c, but only C and C++ | |
3616 | implement build_{binary,unary}_op. Not exactly sure what bits | |
3617 | of functionality are actually needed from those functions, or | |
3618 | where the similar functionality exists in the other front ends. */ | |
3619 | ||
3620 | tree | |
1cae46be | 3621 | expand_tree_builtin (tree function, tree params, tree coerced_params) |
605fb01e | 3622 | { |
605fb01e | 3623 | if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL) |
3624 | return NULL_TREE; | |
3625 | ||
3626 | switch (DECL_FUNCTION_CODE (function)) | |
3627 | { | |
3628 | case BUILT_IN_ABS: | |
d2d4bdde | 3629 | case BUILT_IN_LABS: |
3630 | case BUILT_IN_LLABS: | |
3631 | case BUILT_IN_IMAXABS: | |
605fb01e | 3632 | case BUILT_IN_FABS: |
d2d4bdde | 3633 | case BUILT_IN_FABSL: |
3634 | case BUILT_IN_FABSF: | |
605fb01e | 3635 | if (coerced_params == 0) |
3636 | return integer_zero_node; | |
3637 | return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0); | |
3638 | ||
d30e4d04 | 3639 | case BUILT_IN_CONJ: |
d2d4bdde | 3640 | case BUILT_IN_CONJF: |
3641 | case BUILT_IN_CONJL: | |
d30e4d04 | 3642 | if (coerced_params == 0) |
3643 | return integer_zero_node; | |
3644 | return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0); | |
3645 | ||
3646 | case BUILT_IN_CREAL: | |
d2d4bdde | 3647 | case BUILT_IN_CREALF: |
3648 | case BUILT_IN_CREALL: | |
d30e4d04 | 3649 | if (coerced_params == 0) |
3650 | return integer_zero_node; | |
3651 | return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0); | |
3652 | ||
3653 | case BUILT_IN_CIMAG: | |
d2d4bdde | 3654 | case BUILT_IN_CIMAGF: |
3655 | case BUILT_IN_CIMAGL: | |
d30e4d04 | 3656 | if (coerced_params == 0) |
3657 | return integer_zero_node; | |
3658 | return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0); | |
3659 | ||
605fb01e | 3660 | case BUILT_IN_ISGREATER: |
920d0fb5 | 3661 | return expand_unordered_cmp (function, params, UNLE_EXPR, LE_EXPR); |
605fb01e | 3662 | |
3663 | case BUILT_IN_ISGREATEREQUAL: | |
920d0fb5 | 3664 | return expand_unordered_cmp (function, params, UNLT_EXPR, LT_EXPR); |
605fb01e | 3665 | |
3666 | case BUILT_IN_ISLESS: | |
920d0fb5 | 3667 | return expand_unordered_cmp (function, params, UNGE_EXPR, GE_EXPR); |
605fb01e | 3668 | |
3669 | case BUILT_IN_ISLESSEQUAL: | |
920d0fb5 | 3670 | return expand_unordered_cmp (function, params, UNGT_EXPR, GT_EXPR); |
605fb01e | 3671 | |
3672 | case BUILT_IN_ISLESSGREATER: | |
920d0fb5 | 3673 | return expand_unordered_cmp (function, params, UNEQ_EXPR, EQ_EXPR); |
605fb01e | 3674 | |
3675 | case BUILT_IN_ISUNORDERED: | |
920d0fb5 | 3676 | return expand_unordered_cmp (function, params, UNORDERED_EXPR, NOP_EXPR); |
605fb01e | 3677 | |
3678 | default: | |
3679 | break; | |
3680 | } | |
3681 | ||
3682 | return NULL_TREE; | |
3683 | } | |
51444f0d | 3684 | |
7b915c10 | 3685 | /* Walk the statement tree, rooted at *tp. Apply FUNC to all the |
a08e60ae | 3686 | sub-trees of *TP in a pre-order traversal. FUNC is called with the |
3687 | DATA and the address of each sub-tree. If FUNC returns a non-NULL | |
3688 | value, the traversal is aborted, and the value returned by FUNC is | |
3689 | returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of | |
3690 | the node being visited are not walked. | |
3691 | ||
3692 | We don't need a without_duplicates variant of this one because the | |
3693 | statement tree is a tree, not a graph. */ | |
3694 | ||
1cae46be | 3695 | tree |
3696 | walk_stmt_tree (tree *tp, walk_tree_fn func, void *data) | |
a08e60ae | 3697 | { |
3698 | enum tree_code code; | |
3699 | int walk_subtrees; | |
3700 | tree result; | |
3701 | int i, len; | |
3702 | ||
3703 | #define WALK_SUBTREE(NODE) \ | |
3704 | do \ | |
3705 | { \ | |
3706 | result = walk_stmt_tree (&(NODE), func, data); \ | |
3707 | if (result) \ | |
3708 | return result; \ | |
3709 | } \ | |
3710 | while (0) | |
3711 | ||
3712 | /* Skip empty subtrees. */ | |
3713 | if (!*tp) | |
3714 | return NULL_TREE; | |
3715 | ||
3716 | /* Skip subtrees below non-statement nodes. */ | |
bac62436 | 3717 | if (!STATEMENT_CODE_P (TREE_CODE (*tp))) |
a08e60ae | 3718 | return NULL_TREE; |
3719 | ||
3720 | /* Call the function. */ | |
3721 | walk_subtrees = 1; | |
3722 | result = (*func) (tp, &walk_subtrees, data); | |
3723 | ||
3724 | /* If we found something, return it. */ | |
3725 | if (result) | |
3726 | return result; | |
3727 | ||
a08e60ae | 3728 | /* FUNC may have modified the tree, recheck that we're looking at a |
3729 | statement node. */ | |
3730 | code = TREE_CODE (*tp); | |
bac62436 | 3731 | if (!STATEMENT_CODE_P (code)) |
a08e60ae | 3732 | return NULL_TREE; |
3733 | ||
f2b74df1 | 3734 | /* Visit the subtrees unless FUNC decided that there was nothing |
3735 | interesting below this point in the tree. */ | |
3736 | if (walk_subtrees) | |
3737 | { | |
3738 | /* Walk over all the sub-trees of this operand. Statement nodes | |
3739 | never contain RTL, and we needn't worry about TARGET_EXPRs. */ | |
3740 | len = TREE_CODE_LENGTH (code); | |
3741 | ||
3742 | /* Go through the subtrees. We need to do this in forward order so | |
3743 | that the scope of a FOR_EXPR is handled properly. */ | |
3744 | for (i = 0; i < len; ++i) | |
3745 | WALK_SUBTREE (TREE_OPERAND (*tp, i)); | |
3746 | } | |
a08e60ae | 3747 | |
3748 | /* Finally visit the chain. This can be tail-recursion optimized if | |
3749 | we write it this way. */ | |
3750 | return walk_stmt_tree (&TREE_CHAIN (*tp), func, data); | |
3751 | ||
3752 | #undef WALK_SUBTREE | |
3753 | } | |
3754 | ||
e41f0d80 | 3755 | /* Used to compare case labels. K1 and K2 are actually tree nodes |
3756 | representing case labels, or NULL_TREE for a `default' label. | |
3757 | Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after | |
3758 | K2, and 0 if K1 and K2 are equal. */ | |
3759 | ||
3760 | int | |
1cae46be | 3761 | case_compare (splay_tree_key k1, splay_tree_key k2) |
e41f0d80 | 3762 | { |
3763 | /* Consider a NULL key (such as arises with a `default' label) to be | |
3764 | smaller than anything else. */ | |
3765 | if (!k1) | |
3766 | return k2 ? -1 : 0; | |
3767 | else if (!k2) | |
3768 | return k1 ? 1 : 0; | |
3769 | ||
3770 | return tree_int_cst_compare ((tree) k1, (tree) k2); | |
3771 | } | |
3772 | ||
3773 | /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If | |
3774 | LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is | |
3775 | actually a `default' label. If only HIGH_VALUE is NULL_TREE, then | |
3776 | case label was declared using the usual C/C++ syntax, rather than | |
3777 | the GNU case range extension. CASES is a tree containing all the | |
3778 | case ranges processed so far; COND is the condition for the | |
3779 | switch-statement itself. Returns the CASE_LABEL created, or | |
3780 | ERROR_MARK_NODE if no CASE_LABEL is created. */ | |
3781 | ||
3782 | tree | |
1cae46be | 3783 | c_add_case_label (splay_tree cases, tree cond, tree low_value, |
3784 | tree high_value) | |
e41f0d80 | 3785 | { |
3786 | tree type; | |
3787 | tree label; | |
3788 | tree case_label; | |
3789 | splay_tree_node node; | |
3790 | ||
3791 | /* Create the LABEL_DECL itself. */ | |
3792 | label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); | |
3793 | DECL_CONTEXT (label) = current_function_decl; | |
3794 | ||
3795 | /* If there was an error processing the switch condition, bail now | |
3796 | before we get more confused. */ | |
3797 | if (!cond || cond == error_mark_node) | |
3798 | { | |
3799 | /* Add a label anyhow so that the back-end doesn't think that | |
3800 | the beginning of the switch is unreachable. */ | |
3801 | if (!cases->root) | |
3802 | add_stmt (build_case_label (NULL_TREE, NULL_TREE, label)); | |
3803 | return error_mark_node; | |
3804 | } | |
3805 | ||
1cae46be | 3806 | if ((low_value && TREE_TYPE (low_value) |
3807 | && POINTER_TYPE_P (TREE_TYPE (low_value))) | |
e41f0d80 | 3808 | || (high_value && TREE_TYPE (high_value) |
3809 | && POINTER_TYPE_P (TREE_TYPE (high_value)))) | |
3810 | error ("pointers are not permitted as case values"); | |
3811 | ||
3812 | /* Case ranges are a GNU extension. */ | |
3813 | if (high_value && pedantic) | |
c0f19401 | 3814 | pedwarn ("range expressions in switch statements are non-standard"); |
e41f0d80 | 3815 | |
3816 | type = TREE_TYPE (cond); | |
3817 | if (low_value) | |
3818 | { | |
3819 | low_value = check_case_value (low_value); | |
3820 | low_value = convert_and_check (type, low_value); | |
3821 | } | |
3822 | if (high_value) | |
3823 | { | |
3824 | high_value = check_case_value (high_value); | |
3825 | high_value = convert_and_check (type, high_value); | |
3826 | } | |
3827 | ||
3828 | /* If an error has occurred, bail out now. */ | |
3829 | if (low_value == error_mark_node || high_value == error_mark_node) | |
3830 | { | |
3831 | if (!cases->root) | |
3832 | add_stmt (build_case_label (NULL_TREE, NULL_TREE, label)); | |
3833 | return error_mark_node; | |
3834 | } | |
3835 | ||
3836 | /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't | |
3837 | really a case range, even though it was written that way. Remove | |
3838 | the HIGH_VALUE to simplify later processing. */ | |
3839 | if (tree_int_cst_equal (low_value, high_value)) | |
3840 | high_value = NULL_TREE; | |
1cae46be | 3841 | if (low_value && high_value |
3842 | && !tree_int_cst_lt (low_value, high_value)) | |
e41f0d80 | 3843 | warning ("empty range specified"); |
3844 | ||
3845 | /* Look up the LOW_VALUE in the table of case labels we already | |
3846 | have. */ | |
3847 | node = splay_tree_lookup (cases, (splay_tree_key) low_value); | |
3848 | /* If there was not an exact match, check for overlapping ranges. | |
3849 | There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; | |
3850 | that's a `default' label and the only overlap is an exact match. */ | |
3851 | if (!node && (low_value || high_value)) | |
3852 | { | |
3853 | splay_tree_node low_bound; | |
3854 | splay_tree_node high_bound; | |
3855 | ||
3856 | /* Even though there wasn't an exact match, there might be an | |
3857 | overlap between this case range and another case range. | |
3858 | Since we've (inductively) not allowed any overlapping case | |
3859 | ranges, we simply need to find the greatest low case label | |
3860 | that is smaller that LOW_VALUE, and the smallest low case | |
3861 | label that is greater than LOW_VALUE. If there is an overlap | |
3862 | it will occur in one of these two ranges. */ | |
3863 | low_bound = splay_tree_predecessor (cases, | |
3864 | (splay_tree_key) low_value); | |
3865 | high_bound = splay_tree_successor (cases, | |
3866 | (splay_tree_key) low_value); | |
3867 | ||
3868 | /* Check to see if the LOW_BOUND overlaps. It is smaller than | |
3869 | the LOW_VALUE, so there is no need to check unless the | |
3870 | LOW_BOUND is in fact itself a case range. */ | |
3871 | if (low_bound | |
3872 | && CASE_HIGH ((tree) low_bound->value) | |
3873 | && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), | |
3874 | low_value) >= 0) | |
3875 | node = low_bound; | |
3876 | /* Check to see if the HIGH_BOUND overlaps. The low end of that | |
3877 | range is bigger than the low end of the current range, so we | |
3878 | are only interested if the current range is a real range, and | |
3879 | not an ordinary case label. */ | |
1cae46be | 3880 | else if (high_bound |
e41f0d80 | 3881 | && high_value |
3882 | && (tree_int_cst_compare ((tree) high_bound->key, | |
3883 | high_value) | |
3884 | <= 0)) | |
3885 | node = high_bound; | |
3886 | } | |
3887 | /* If there was an overlap, issue an error. */ | |
3888 | if (node) | |
3889 | { | |
3890 | tree duplicate = CASE_LABEL_DECL ((tree) node->value); | |
3891 | ||
3892 | if (high_value) | |
3893 | { | |
3894 | error ("duplicate (or overlapping) case value"); | |
f2791601 | 3895 | error ("%Hthis is the first entry overlapping that value", |
3896 | &DECL_SOURCE_LOCATION (duplicate)); | |
e41f0d80 | 3897 | } |
3898 | else if (low_value) | |
3899 | { | |
3900 | error ("duplicate case value") ; | |
f2791601 | 3901 | error ("%Hpreviously used here", &DECL_SOURCE_LOCATION (duplicate)); |
e41f0d80 | 3902 | } |
3903 | else | |
3904 | { | |
3905 | error ("multiple default labels in one switch"); | |
f2791601 | 3906 | error ("%Hthis is the first default label", |
3907 | &DECL_SOURCE_LOCATION (duplicate)); | |
e41f0d80 | 3908 | } |
3909 | if (!cases->root) | |
3910 | add_stmt (build_case_label (NULL_TREE, NULL_TREE, label)); | |
3911 | } | |
3912 | ||
3913 | /* Add a CASE_LABEL to the statement-tree. */ | |
3914 | case_label = add_stmt (build_case_label (low_value, high_value, label)); | |
3915 | /* Register this case label in the splay tree. */ | |
1cae46be | 3916 | splay_tree_insert (cases, |
e41f0d80 | 3917 | (splay_tree_key) low_value, |
3918 | (splay_tree_value) case_label); | |
3919 | ||
3920 | return case_label; | |
3921 | } | |
3922 | ||
9dd48740 | 3923 | /* Finish an expression taking the address of LABEL (an |
3924 | IDENTIFIER_NODE). Returns an expression for the address. */ | |
d0a47c8d | 3925 | |
1cae46be | 3926 | tree |
3927 | finish_label_address_expr (tree label) | |
d0a47c8d | 3928 | { |
3929 | tree result; | |
3930 | ||
3931 | if (pedantic) | |
c0f19401 | 3932 | pedwarn ("taking the address of a label is non-standard"); |
d0a47c8d | 3933 | |
9dd48740 | 3934 | if (label == error_mark_node) |
3935 | return error_mark_node; | |
3936 | ||
d0a47c8d | 3937 | label = lookup_label (label); |
3938 | if (label == NULL_TREE) | |
3939 | result = null_pointer_node; | |
3940 | else | |
3941 | { | |
3942 | TREE_USED (label) = 1; | |
3943 | result = build1 (ADDR_EXPR, ptr_type_node, label); | |
3944 | TREE_CONSTANT (result) = 1; | |
3945 | /* The current function in not necessarily uninlinable. | |
3946 | Computed gotos are incompatible with inlining, but the value | |
3947 | here could be used only in a diagnostic, for example. */ | |
3948 | } | |
3949 | ||
3950 | return result; | |
3951 | } | |
3952 | ||
e41f0d80 | 3953 | /* Hook used by expand_expr to expand language-specific tree codes. */ |
3954 | ||
3955 | rtx | |
1cae46be | 3956 | c_expand_expr (tree exp, rtx target, enum machine_mode tmode, int modifier) |
3957 | /* Actually enum_modifier. */ | |
e41f0d80 | 3958 | { |
3959 | switch (TREE_CODE (exp)) | |
3960 | { | |
3961 | case STMT_EXPR: | |
3962 | { | |
3963 | tree rtl_expr; | |
3964 | rtx result; | |
da3ac634 | 3965 | bool preserve_result = false; |
dc8c3dcf | 3966 | bool return_target = false; |
e41f0d80 | 3967 | |
3968 | /* Since expand_expr_stmt calls free_temp_slots after every | |
3969 | expression statement, we must call push_temp_slots here. | |
3970 | Otherwise, any temporaries in use now would be considered | |
3971 | out-of-scope after the first EXPR_STMT from within the | |
3972 | STMT_EXPR. */ | |
3973 | push_temp_slots (); | |
7e83d989 | 3974 | rtl_expr = expand_start_stmt_expr (!STMT_EXPR_NO_SCOPE (exp)); |
d513ec2f | 3975 | |
3976 | /* If we want the result of this expression, find the last | |
3977 | EXPR_STMT in the COMPOUND_STMT and mark it as addressable. */ | |
64fbdcbf | 3978 | if (target != const0_rtx |
3979 | && TREE_CODE (STMT_EXPR_STMT (exp)) == COMPOUND_STMT | |
3980 | && TREE_CODE (COMPOUND_BODY (STMT_EXPR_STMT (exp))) == SCOPE_STMT) | |
d513ec2f | 3981 | { |
64fbdcbf | 3982 | tree expr = COMPOUND_BODY (STMT_EXPR_STMT (exp)); |
3983 | tree last = TREE_CHAIN (expr); | |
d513ec2f | 3984 | |
64fbdcbf | 3985 | while (TREE_CHAIN (last)) |
d513ec2f | 3986 | { |
64fbdcbf | 3987 | expr = last; |
3988 | last = TREE_CHAIN (last); | |
d513ec2f | 3989 | } |
64fbdcbf | 3990 | |
3991 | if (TREE_CODE (last) == SCOPE_STMT | |
3992 | && TREE_CODE (expr) == EXPR_STMT) | |
da3ac634 | 3993 | { |
dc8c3dcf | 3994 | if (target && TREE_CODE (EXPR_STMT_EXPR (expr)) == VAR_DECL |
3995 | && DECL_RTL_IF_SET (EXPR_STMT_EXPR (expr)) == target) | |
3996 | /* If the last expression is a variable whose RTL is the | |
3997 | same as our target, just return the target; if it | |
3998 | isn't valid expanding the decl would produce different | |
3999 | RTL, and store_expr would try to do a copy. */ | |
4000 | return_target = true; | |
4001 | else | |
4002 | { | |
4003 | /* Otherwise, note that we want the value from the last | |
4004 | expression. */ | |
4005 | TREE_ADDRESSABLE (expr) = 1; | |
4006 | preserve_result = true; | |
4007 | } | |
da3ac634 | 4008 | } |
d513ec2f | 4009 | } |
4010 | ||
e41f0d80 | 4011 | expand_stmt (STMT_EXPR_STMT (exp)); |
4012 | expand_end_stmt_expr (rtl_expr); | |
da3ac634 | 4013 | |
e41f0d80 | 4014 | result = expand_expr (rtl_expr, target, tmode, modifier); |
dc8c3dcf | 4015 | if (return_target) |
4016 | result = target; | |
4017 | else if (preserve_result && GET_CODE (result) == MEM) | |
da3ac634 | 4018 | { |
4019 | if (GET_MODE (result) != BLKmode) | |
4020 | result = copy_to_reg (result); | |
4021 | else | |
4022 | preserve_temp_slots (result); | |
4023 | } | |
4024 | ||
7e83d989 | 4025 | /* If the statment-expression does not have a scope, then the |
4026 | new temporaries we created within it must live beyond the | |
4027 | statement-expression. */ | |
4028 | if (STMT_EXPR_NO_SCOPE (exp)) | |
4029 | preserve_temp_slots (NULL_RTX); | |
4030 | ||
e41f0d80 | 4031 | pop_temp_slots (); |
4032 | return result; | |
4033 | } | |
4034 | break; | |
1cae46be | 4035 | |
ec11e38e | 4036 | case COMPOUND_LITERAL_EXPR: |
4037 | { | |
4038 | /* Initialize the anonymous variable declared in the compound | |
4039 | literal, then return the variable. */ | |
4040 | tree decl = COMPOUND_LITERAL_EXPR_DECL (exp); | |
4041 | emit_local_var (decl); | |
4042 | return expand_expr (decl, target, tmode, modifier); | |
4043 | } | |
4044 | ||
e41f0d80 | 4045 | default: |
4046 | abort (); | |
4047 | } | |
4048 | ||
4049 | abort (); | |
4050 | return NULL; | |
4051 | } | |
4052 | ||
4053 | /* Hook used by safe_from_p to handle language-specific tree codes. */ | |
4054 | ||
4055 | int | |
1cae46be | 4056 | c_safe_from_p (rtx target, tree exp) |
e41f0d80 | 4057 | { |
4058 | /* We can see statements here when processing the body of a | |
4059 | statement-expression. For a declaration statement declaring a | |
4060 | variable, look at the variable's initializer. */ | |
1cae46be | 4061 | if (TREE_CODE (exp) == DECL_STMT) |
e41f0d80 | 4062 | { |
4063 | tree decl = DECL_STMT_DECL (exp); | |
4064 | ||
4065 | if (TREE_CODE (decl) == VAR_DECL | |
4066 | && DECL_INITIAL (decl) | |
4067 | && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0)) | |
4068 | return 0; | |
4069 | } | |
4070 | ||
4071 | /* For any statement, we must follow the statement-chain. */ | |
bac62436 | 4072 | if (STATEMENT_CODE_P (TREE_CODE (exp)) && TREE_CHAIN (exp)) |
e41f0d80 | 4073 | return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0); |
4074 | ||
4075 | /* Assume everything else is safe. */ | |
4076 | return 1; | |
4077 | } | |
4078 | ||
c63e27d8 | 4079 | /* Hook used by unsafe_for_reeval to handle language-specific tree codes. */ |
4080 | ||
4081 | int | |
1cae46be | 4082 | c_common_unsafe_for_reeval (tree exp) |
c63e27d8 | 4083 | { |
0e62d8da | 4084 | /* Statement expressions may not be reevaluated, likewise compound |
4085 | literals. */ | |
4086 | if (TREE_CODE (exp) == STMT_EXPR | |
4087 | || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) | |
c63e27d8 | 4088 | return 2; |
4089 | ||
4090 | /* Walk all other expressions. */ | |
4091 | return -1; | |
4092 | } | |
4093 | ||
ec11e38e | 4094 | /* Hook used by staticp to handle language-specific tree codes. */ |
4095 | ||
4096 | int | |
1cae46be | 4097 | c_staticp (tree exp) |
ec11e38e | 4098 | { |
4099 | if (TREE_CODE (exp) == COMPOUND_LITERAL_EXPR | |
4100 | && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp))) | |
4101 | return 1; | |
4102 | return 0; | |
4103 | } | |
4f9a1c9b | 4104 | \f |
4105 | ||
4106 | /* Given a boolean expression ARG, return a tree representing an increment | |
4107 | or decrement (as indicated by CODE) of ARG. The front end must check for | |
4108 | invalid cases (e.g., decrement in C++). */ | |
4109 | tree | |
1cae46be | 4110 | boolean_increment (enum tree_code code, tree arg) |
4f9a1c9b | 4111 | { |
4112 | tree val; | |
3c2239cf | 4113 | tree true_res = boolean_true_node; |
c0f19401 | 4114 | |
4f9a1c9b | 4115 | arg = stabilize_reference (arg); |
4116 | switch (code) | |
4117 | { | |
4118 | case PREINCREMENT_EXPR: | |
4119 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); | |
4120 | break; | |
4121 | case POSTINCREMENT_EXPR: | |
4122 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); | |
4123 | arg = save_expr (arg); | |
4124 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); | |
4125 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4126 | break; | |
4127 | case PREDECREMENT_EXPR: | |
4128 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg)); | |
4129 | break; | |
4130 | case POSTDECREMENT_EXPR: | |
4131 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg)); | |
4132 | arg = save_expr (arg); | |
4133 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); | |
4134 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4135 | break; | |
4136 | default: | |
4137 | abort (); | |
4138 | } | |
4139 | TREE_SIDE_EFFECTS (val) = 1; | |
4140 | return val; | |
4141 | } | |
76a6e674 | 4142 | \f |
fd6f6435 | 4143 | /* Built-in macros for stddef.h, that require macros defined in this |
4144 | file. */ | |
79cf3ec1 | 4145 | void |
1cae46be | 4146 | c_stddef_cpp_builtins(void) |
1ed9d5f5 | 4147 | { |
63994318 | 4148 | builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0); |
4149 | builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0); | |
4150 | builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0); | |
4151 | builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0); | |
574006c3 | 4152 | } |
4153 | ||
7d3b509a | 4154 | static void |
1cae46be | 4155 | c_init_attributes (void) |
7d3b509a | 4156 | { |
4157 | /* Fill in the built_in_attributes array. */ | |
4158 | #define DEF_ATTR_NULL_TREE(ENUM) \ | |
4159 | built_in_attributes[(int) ENUM] = NULL_TREE; | |
4160 | #define DEF_ATTR_INT(ENUM, VALUE) \ | |
4161 | built_in_attributes[(int) ENUM] = build_int_2 (VALUE, VALUE < 0 ? -1 : 0); | |
4162 | #define DEF_ATTR_IDENT(ENUM, STRING) \ | |
4163 | built_in_attributes[(int) ENUM] = get_identifier (STRING); | |
4164 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \ | |
4165 | built_in_attributes[(int) ENUM] \ | |
4166 | = tree_cons (built_in_attributes[(int) PURPOSE], \ | |
4167 | built_in_attributes[(int) VALUE], \ | |
4168 | built_in_attributes[(int) CHAIN]); | |
7d3b509a | 4169 | #include "builtin-attrs.def" |
4170 | #undef DEF_ATTR_NULL_TREE | |
4171 | #undef DEF_ATTR_INT | |
4172 | #undef DEF_ATTR_IDENT | |
4173 | #undef DEF_ATTR_TREE_LIST | |
76a6e674 | 4174 | } |
5f3cead1 | 4175 | |
e48d0f41 | 4176 | /* Output a -Wshadow warning MSGCODE about NAME, and give the location |
31ddae9f | 4177 | of the previous declaration DECL. */ |
5f3cead1 | 4178 | void |
1cae46be | 4179 | shadow_warning (enum sw_kind msgcode, const char *name, tree decl) |
5f3cead1 | 4180 | { |
e48d0f41 | 4181 | static const char *const msgs[] = { |
4182 | /* SW_PARAM */ N_("declaration of \"%s\" shadows a parameter"), | |
4183 | /* SW_LOCAL */ N_("declaration of \"%s\" shadows a previous local"), | |
4184 | /* SW_GLOBAL */ N_("declaration of \"%s\" shadows a global declaration") | |
4185 | }; | |
4186 | ||
31ddae9f | 4187 | warning (msgs[msgcode], name); |
834fc4a0 | 4188 | warning ("%Hshadowed declaration is here", &DECL_SOURCE_LOCATION (decl)); |
5f3cead1 | 4189 | } |
4190 | ||
f8e93a2e | 4191 | /* Attribute handlers common to C front ends. */ |
4192 | ||
4193 | /* Handle a "packed" attribute; arguments as in | |
4194 | struct attribute_spec.handler. */ | |
4195 | ||
4196 | static tree | |
1cae46be | 4197 | handle_packed_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED, |
09347743 | 4198 | int flags, bool *no_add_attrs) |
f8e93a2e | 4199 | { |
f40175cb | 4200 | if (TYPE_P (*node)) |
f8e93a2e | 4201 | { |
4202 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
f40175cb | 4203 | *node = build_type_copy (*node); |
4204 | TYPE_PACKED (*node) = 1; | |
4205 | if (TYPE_MAIN_VARIANT (*node) == *node) | |
4206 | { | |
4207 | /* If it is the main variant, then pack the other variants | |
4208 | too. This happens in, | |
4209 | ||
4210 | struct Foo { | |
4211 | struct Foo const *ptr; // creates a variant w/o packed flag | |
4212 | } __ attribute__((packed)); // packs it now. | |
4213 | */ | |
4214 | tree probe; | |
4215 | ||
4216 | for (probe = *node; probe; probe = TYPE_NEXT_VARIANT (probe)) | |
4217 | TYPE_PACKED (probe) = 1; | |
4218 | } | |
4219 | ||
f8e93a2e | 4220 | } |
4221 | else if (TREE_CODE (*node) == FIELD_DECL) | |
4222 | DECL_PACKED (*node) = 1; | |
4223 | /* We can't set DECL_PACKED for a VAR_DECL, because the bit is | |
f40175cb | 4224 | used for DECL_REGISTER. It wouldn't mean anything anyway. |
4225 | We can't set DECL_PACKED on the type of a TYPE_DECL, because | |
4226 | that changes what the typedef is typing. */ | |
f8e93a2e | 4227 | else |
4228 | { | |
4229 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4230 | *no_add_attrs = true; | |
4231 | } | |
4232 | ||
4233 | return NULL_TREE; | |
4234 | } | |
4235 | ||
4236 | /* Handle a "nocommon" attribute; arguments as in | |
4237 | struct attribute_spec.handler. */ | |
4238 | ||
4239 | static tree | |
1cae46be | 4240 | handle_nocommon_attribute (tree *node, tree name, |
4241 | tree args ATTRIBUTE_UNUSED, | |
09347743 | 4242 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4243 | { |
4244 | if (TREE_CODE (*node) == VAR_DECL) | |
4245 | DECL_COMMON (*node) = 0; | |
4246 | else | |
4247 | { | |
4248 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4249 | *no_add_attrs = true; | |
4250 | } | |
4251 | ||
4252 | return NULL_TREE; | |
4253 | } | |
4254 | ||
4255 | /* Handle a "common" attribute; arguments as in | |
4256 | struct attribute_spec.handler. */ | |
4257 | ||
4258 | static tree | |
1cae46be | 4259 | handle_common_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED, |
09347743 | 4260 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4261 | { |
4262 | if (TREE_CODE (*node) == VAR_DECL) | |
4263 | DECL_COMMON (*node) = 1; | |
4264 | else | |
4265 | { | |
4266 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4267 | *no_add_attrs = true; | |
4268 | } | |
4269 | ||
4270 | return NULL_TREE; | |
4271 | } | |
4272 | ||
4273 | /* Handle a "noreturn" attribute; arguments as in | |
4274 | struct attribute_spec.handler. */ | |
4275 | ||
4276 | static tree | |
1cae46be | 4277 | handle_noreturn_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED, |
09347743 | 4278 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4279 | { |
4280 | tree type = TREE_TYPE (*node); | |
4281 | ||
4282 | /* See FIXME comment in c_common_attribute_table. */ | |
4283 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
4284 | TREE_THIS_VOLATILE (*node) = 1; | |
4285 | else if (TREE_CODE (type) == POINTER_TYPE | |
4286 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
4287 | TREE_TYPE (*node) | |
4288 | = build_pointer_type | |
4289 | (build_type_variant (TREE_TYPE (type), | |
4290 | TREE_READONLY (TREE_TYPE (type)), 1)); | |
4291 | else | |
4292 | { | |
4293 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4294 | *no_add_attrs = true; | |
4295 | } | |
4296 | ||
4297 | return NULL_TREE; | |
4298 | } | |
4299 | ||
4300 | /* Handle a "noinline" attribute; arguments as in | |
4301 | struct attribute_spec.handler. */ | |
4302 | ||
4303 | static tree | |
1cae46be | 4304 | handle_noinline_attribute (tree *node, tree name, |
4305 | tree args ATTRIBUTE_UNUSED, | |
09347743 | 4306 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4307 | { |
4308 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
4309 | DECL_UNINLINABLE (*node) = 1; | |
4310 | else | |
4311 | { | |
4312 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4313 | *no_add_attrs = true; | |
4314 | } | |
4315 | ||
4316 | return NULL_TREE; | |
4317 | } | |
4318 | ||
4319 | /* Handle a "always_inline" attribute; arguments as in | |
4320 | struct attribute_spec.handler. */ | |
4321 | ||
4322 | static tree | |
1cae46be | 4323 | handle_always_inline_attribute (tree *node, tree name, |
4324 | tree args ATTRIBUTE_UNUSED, | |
4325 | int flags ATTRIBUTE_UNUSED, | |
09347743 | 4326 | bool *no_add_attrs) |
f8e93a2e | 4327 | { |
4328 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
4329 | { | |
4330 | /* Do nothing else, just set the attribute. We'll get at | |
4331 | it later with lookup_attribute. */ | |
4332 | } | |
4333 | else | |
4334 | { | |
4335 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4336 | *no_add_attrs = true; | |
4337 | } | |
4338 | ||
4339 | return NULL_TREE; | |
4340 | } | |
4341 | ||
4342 | /* Handle a "used" attribute; arguments as in | |
4343 | struct attribute_spec.handler. */ | |
4344 | ||
4345 | static tree | |
1cae46be | 4346 | handle_used_attribute (tree *pnode, tree name, tree args ATTRIBUTE_UNUSED, |
09347743 | 4347 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4348 | { |
d0a31bd8 | 4349 | tree node = *pnode; |
4350 | ||
4351 | if (TREE_CODE (node) == FUNCTION_DECL | |
4352 | || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))) | |
f54ed8bc | 4353 | { |
f54ed8bc | 4354 | TREE_USED (node) = 1; |
4355 | } | |
f8e93a2e | 4356 | else |
4357 | { | |
4358 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4359 | *no_add_attrs = true; | |
4360 | } | |
4361 | ||
4362 | return NULL_TREE; | |
4363 | } | |
4364 | ||
4365 | /* Handle a "unused" attribute; arguments as in | |
4366 | struct attribute_spec.handler. */ | |
4367 | ||
4368 | static tree | |
1cae46be | 4369 | handle_unused_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED, |
09347743 | 4370 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4371 | { |
4372 | if (DECL_P (*node)) | |
4373 | { | |
4374 | tree decl = *node; | |
4375 | ||
4376 | if (TREE_CODE (decl) == PARM_DECL | |
4377 | || TREE_CODE (decl) == VAR_DECL | |
4378 | || TREE_CODE (decl) == FUNCTION_DECL | |
4379 | || TREE_CODE (decl) == LABEL_DECL | |
4380 | || TREE_CODE (decl) == TYPE_DECL) | |
4381 | TREE_USED (decl) = 1; | |
4382 | else | |
4383 | { | |
4384 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4385 | *no_add_attrs = true; | |
4386 | } | |
4387 | } | |
4388 | else | |
4389 | { | |
4390 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
4391 | *node = build_type_copy (*node); | |
4392 | TREE_USED (*node) = 1; | |
4393 | } | |
4394 | ||
4395 | return NULL_TREE; | |
4396 | } | |
4397 | ||
4398 | /* Handle a "const" attribute; arguments as in | |
4399 | struct attribute_spec.handler. */ | |
4400 | ||
4401 | static tree | |
1cae46be | 4402 | handle_const_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED, |
09347743 | 4403 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4404 | { |
4405 | tree type = TREE_TYPE (*node); | |
4406 | ||
4407 | /* See FIXME comment on noreturn in c_common_attribute_table. */ | |
4408 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
4409 | TREE_READONLY (*node) = 1; | |
4410 | else if (TREE_CODE (type) == POINTER_TYPE | |
4411 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
4412 | TREE_TYPE (*node) | |
4413 | = build_pointer_type | |
4414 | (build_type_variant (TREE_TYPE (type), 1, | |
4415 | TREE_THIS_VOLATILE (TREE_TYPE (type)))); | |
4416 | else | |
4417 | { | |
4418 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4419 | *no_add_attrs = true; | |
4420 | } | |
4421 | ||
4422 | return NULL_TREE; | |
4423 | } | |
4424 | ||
4425 | /* Handle a "transparent_union" attribute; arguments as in | |
4426 | struct attribute_spec.handler. */ | |
4427 | ||
4428 | static tree | |
1cae46be | 4429 | handle_transparent_union_attribute (tree *node, tree name, |
4430 | tree args ATTRIBUTE_UNUSED, int flags, | |
09347743 | 4431 | bool *no_add_attrs) |
f8e93a2e | 4432 | { |
4433 | tree decl = NULL_TREE; | |
4434 | tree *type = NULL; | |
4435 | int is_type = 0; | |
4436 | ||
4437 | if (DECL_P (*node)) | |
4438 | { | |
4439 | decl = *node; | |
4440 | type = &TREE_TYPE (decl); | |
4441 | is_type = TREE_CODE (*node) == TYPE_DECL; | |
4442 | } | |
4443 | else if (TYPE_P (*node)) | |
4444 | type = node, is_type = 1; | |
4445 | ||
4446 | if (is_type | |
4447 | && TREE_CODE (*type) == UNION_TYPE | |
4448 | && (decl == 0 | |
4449 | || (TYPE_FIELDS (*type) != 0 | |
4450 | && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type))))) | |
4451 | { | |
4452 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
4453 | *type = build_type_copy (*type); | |
4454 | TYPE_TRANSPARENT_UNION (*type) = 1; | |
4455 | } | |
4456 | else if (decl != 0 && TREE_CODE (decl) == PARM_DECL | |
4457 | && TREE_CODE (*type) == UNION_TYPE | |
4458 | && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type))) | |
4459 | DECL_TRANSPARENT_UNION (decl) = 1; | |
4460 | else | |
4461 | { | |
4462 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4463 | *no_add_attrs = true; | |
4464 | } | |
4465 | ||
4466 | return NULL_TREE; | |
4467 | } | |
4468 | ||
4469 | /* Handle a "constructor" attribute; arguments as in | |
4470 | struct attribute_spec.handler. */ | |
4471 | ||
4472 | static tree | |
1cae46be | 4473 | handle_constructor_attribute (tree *node, tree name, |
4474 | tree args ATTRIBUTE_UNUSED, | |
4475 | int flags ATTRIBUTE_UNUSED, | |
09347743 | 4476 | bool *no_add_attrs) |
f8e93a2e | 4477 | { |
4478 | tree decl = *node; | |
4479 | tree type = TREE_TYPE (decl); | |
4480 | ||
4481 | if (TREE_CODE (decl) == FUNCTION_DECL | |
4482 | && TREE_CODE (type) == FUNCTION_TYPE | |
4483 | && decl_function_context (decl) == 0) | |
4484 | { | |
4485 | DECL_STATIC_CONSTRUCTOR (decl) = 1; | |
4486 | TREE_USED (decl) = 1; | |
4487 | } | |
4488 | else | |
4489 | { | |
4490 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4491 | *no_add_attrs = true; | |
4492 | } | |
4493 | ||
4494 | return NULL_TREE; | |
4495 | } | |
4496 | ||
4497 | /* Handle a "destructor" attribute; arguments as in | |
4498 | struct attribute_spec.handler. */ | |
4499 | ||
4500 | static tree | |
1cae46be | 4501 | handle_destructor_attribute (tree *node, tree name, |
4502 | tree args ATTRIBUTE_UNUSED, | |
4503 | int flags ATTRIBUTE_UNUSED, | |
09347743 | 4504 | bool *no_add_attrs) |
f8e93a2e | 4505 | { |
4506 | tree decl = *node; | |
4507 | tree type = TREE_TYPE (decl); | |
4508 | ||
4509 | if (TREE_CODE (decl) == FUNCTION_DECL | |
4510 | && TREE_CODE (type) == FUNCTION_TYPE | |
4511 | && decl_function_context (decl) == 0) | |
4512 | { | |
4513 | DECL_STATIC_DESTRUCTOR (decl) = 1; | |
4514 | TREE_USED (decl) = 1; | |
4515 | } | |
4516 | else | |
4517 | { | |
4518 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4519 | *no_add_attrs = true; | |
4520 | } | |
4521 | ||
4522 | return NULL_TREE; | |
4523 | } | |
4524 | ||
4525 | /* Handle a "mode" attribute; arguments as in | |
4526 | struct attribute_spec.handler. */ | |
4527 | ||
4528 | static tree | |
1cae46be | 4529 | handle_mode_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED, |
09347743 | 4530 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4531 | { |
4532 | tree type = *node; | |
4533 | ||
4534 | *no_add_attrs = true; | |
4535 | ||
4536 | if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE) | |
4537 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4538 | else | |
4539 | { | |
4540 | int j; | |
4541 | const char *p = IDENTIFIER_POINTER (TREE_VALUE (args)); | |
4542 | int len = strlen (p); | |
4543 | enum machine_mode mode = VOIDmode; | |
4544 | tree typefm; | |
805e22b2 | 4545 | tree ptr_type; |
f8e93a2e | 4546 | |
4547 | if (len > 4 && p[0] == '_' && p[1] == '_' | |
4548 | && p[len - 1] == '_' && p[len - 2] == '_') | |
4549 | { | |
f0af5a88 | 4550 | char *newp = alloca (len - 1); |
f8e93a2e | 4551 | |
4552 | strcpy (newp, &p[2]); | |
4553 | newp[len - 4] = '\0'; | |
4554 | p = newp; | |
4555 | } | |
4556 | ||
4557 | /* Change this type to have a type with the specified mode. | |
4558 | First check for the special modes. */ | |
4559 | if (! strcmp (p, "byte")) | |
4560 | mode = byte_mode; | |
4561 | else if (!strcmp (p, "word")) | |
4562 | mode = word_mode; | |
4563 | else if (! strcmp (p, "pointer")) | |
4564 | mode = ptr_mode; | |
4565 | else | |
4566 | for (j = 0; j < NUM_MACHINE_MODES; j++) | |
4567 | if (!strcmp (p, GET_MODE_NAME (j))) | |
4568 | mode = (enum machine_mode) j; | |
4569 | ||
4570 | if (mode == VOIDmode) | |
4571 | error ("unknown machine mode `%s'", p); | |
4572 | else if (0 == (typefm = (*lang_hooks.types.type_for_mode) | |
4573 | (mode, TREE_UNSIGNED (type)))) | |
4574 | error ("no data type for mode `%s'", p); | |
805e22b2 | 4575 | else if ((TREE_CODE (type) == POINTER_TYPE |
4576 | || TREE_CODE (type) == REFERENCE_TYPE) | |
4577 | && !(*targetm.valid_pointer_mode) (mode)) | |
4578 | error ("invalid pointer mode `%s'", p); | |
f8e93a2e | 4579 | else |
ead34f59 | 4580 | { |
4581 | /* If this is a vector, make sure we either have hardware | |
4582 | support, or we can emulate it. */ | |
903669fa | 4583 | if (VECTOR_MODE_P (mode) && !vector_mode_valid_p (mode)) |
ead34f59 | 4584 | { |
4585 | error ("unable to emulate '%s'", GET_MODE_NAME (mode)); | |
4586 | return NULL_TREE; | |
4587 | } | |
4588 | ||
805e22b2 | 4589 | if (TREE_CODE (type) == POINTER_TYPE) |
4590 | { | |
4591 | ptr_type = build_pointer_type_for_mode (TREE_TYPE (type), | |
4592 | mode); | |
4593 | *node = ptr_type; | |
4594 | } | |
4595 | else if (TREE_CODE (type) == REFERENCE_TYPE) | |
4596 | { | |
4597 | ptr_type = build_reference_type_for_mode (TREE_TYPE (type), | |
4598 | mode); | |
4599 | *node = ptr_type; | |
4600 | } | |
4601 | else | |
ead34f59 | 4602 | *node = typefm; |
4603 | /* No need to layout the type here. The caller should do this. */ | |
4604 | } | |
f8e93a2e | 4605 | } |
4606 | ||
4607 | return NULL_TREE; | |
4608 | } | |
4609 | ||
4610 | /* Handle a "section" attribute; arguments as in | |
4611 | struct attribute_spec.handler. */ | |
4612 | ||
4613 | static tree | |
1cae46be | 4614 | handle_section_attribute (tree *node, tree name ATTRIBUTE_UNUSED, tree args, |
09347743 | 4615 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4616 | { |
4617 | tree decl = *node; | |
4618 | ||
4619 | if (targetm.have_named_sections) | |
4620 | { | |
4621 | if ((TREE_CODE (decl) == FUNCTION_DECL | |
4622 | || TREE_CODE (decl) == VAR_DECL) | |
4623 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
4624 | { | |
4625 | if (TREE_CODE (decl) == VAR_DECL | |
4626 | && current_function_decl != NULL_TREE | |
4627 | && ! TREE_STATIC (decl)) | |
4628 | { | |
f2791601 | 4629 | error ("%Hsection attribute cannot be specified for " |
4630 | "local variables", &DECL_SOURCE_LOCATION (decl)); | |
f8e93a2e | 4631 | *no_add_attrs = true; |
4632 | } | |
4633 | ||
4634 | /* The decl may have already been given a section attribute | |
4635 | from a previous declaration. Ensure they match. */ | |
4636 | else if (DECL_SECTION_NAME (decl) != NULL_TREE | |
4637 | && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), | |
4638 | TREE_STRING_POINTER (TREE_VALUE (args))) != 0) | |
4639 | { | |
f2791601 | 4640 | error ("%Hsection of '%D' conflicts with previous declaration", |
4641 | &DECL_SOURCE_LOCATION (*node), *node); | |
f8e93a2e | 4642 | *no_add_attrs = true; |
4643 | } | |
4644 | else | |
4645 | DECL_SECTION_NAME (decl) = TREE_VALUE (args); | |
4646 | } | |
4647 | else | |
4648 | { | |
f2791601 | 4649 | error ("%Hsection attribute not allowed for '%D'", |
4650 | &DECL_SOURCE_LOCATION (*node), *node); | |
f8e93a2e | 4651 | *no_add_attrs = true; |
4652 | } | |
4653 | } | |
4654 | else | |
4655 | { | |
f2791601 | 4656 | error ("%Hsection attributes are not supported for this target", |
4657 | &DECL_SOURCE_LOCATION (*node)); | |
f8e93a2e | 4658 | *no_add_attrs = true; |
4659 | } | |
4660 | ||
4661 | return NULL_TREE; | |
4662 | } | |
4663 | ||
4664 | /* Handle a "aligned" attribute; arguments as in | |
4665 | struct attribute_spec.handler. */ | |
4666 | ||
4667 | static tree | |
1cae46be | 4668 | handle_aligned_attribute (tree *node, tree name ATTRIBUTE_UNUSED, tree args, |
09347743 | 4669 | int flags, bool *no_add_attrs) |
f8e93a2e | 4670 | { |
4671 | tree decl = NULL_TREE; | |
4672 | tree *type = NULL; | |
4673 | int is_type = 0; | |
4674 | tree align_expr = (args ? TREE_VALUE (args) | |
4675 | : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT)); | |
4676 | int i; | |
4677 | ||
4678 | if (DECL_P (*node)) | |
4679 | { | |
4680 | decl = *node; | |
4681 | type = &TREE_TYPE (decl); | |
4682 | is_type = TREE_CODE (*node) == TYPE_DECL; | |
4683 | } | |
4684 | else if (TYPE_P (*node)) | |
4685 | type = node, is_type = 1; | |
4686 | ||
4687 | /* Strip any NOPs of any kind. */ | |
4688 | while (TREE_CODE (align_expr) == NOP_EXPR | |
4689 | || TREE_CODE (align_expr) == CONVERT_EXPR | |
4690 | || TREE_CODE (align_expr) == NON_LVALUE_EXPR) | |
4691 | align_expr = TREE_OPERAND (align_expr, 0); | |
4692 | ||
4693 | if (TREE_CODE (align_expr) != INTEGER_CST) | |
4694 | { | |
4695 | error ("requested alignment is not a constant"); | |
4696 | *no_add_attrs = true; | |
4697 | } | |
4698 | else if ((i = tree_log2 (align_expr)) == -1) | |
4699 | { | |
4700 | error ("requested alignment is not a power of 2"); | |
4701 | *no_add_attrs = true; | |
4702 | } | |
4703 | else if (i > HOST_BITS_PER_INT - 2) | |
4704 | { | |
4705 | error ("requested alignment is too large"); | |
4706 | *no_add_attrs = true; | |
4707 | } | |
4708 | else if (is_type) | |
4709 | { | |
4710 | /* If we have a TYPE_DECL, then copy the type, so that we | |
4711 | don't accidentally modify a builtin type. See pushdecl. */ | |
4712 | if (decl && TREE_TYPE (decl) != error_mark_node | |
4713 | && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) | |
4714 | { | |
4715 | tree tt = TREE_TYPE (decl); | |
4716 | *type = build_type_copy (*type); | |
4717 | DECL_ORIGINAL_TYPE (decl) = tt; | |
4718 | TYPE_NAME (*type) = decl; | |
4719 | TREE_USED (*type) = TREE_USED (decl); | |
4720 | TREE_TYPE (decl) = *type; | |
4721 | } | |
4722 | else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
4723 | *type = build_type_copy (*type); | |
4724 | ||
4725 | TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT; | |
4726 | TYPE_USER_ALIGN (*type) = 1; | |
4727 | } | |
4728 | else if (TREE_CODE (decl) != VAR_DECL | |
4729 | && TREE_CODE (decl) != FIELD_DECL) | |
4730 | { | |
f2791601 | 4731 | error ("%Halignment may not be specified for '%D'", |
4732 | &DECL_SOURCE_LOCATION (decl), decl); | |
f8e93a2e | 4733 | *no_add_attrs = true; |
4734 | } | |
4735 | else | |
4736 | { | |
4737 | DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT; | |
4738 | DECL_USER_ALIGN (decl) = 1; | |
4739 | } | |
4740 | ||
4741 | return NULL_TREE; | |
4742 | } | |
4743 | ||
4744 | /* Handle a "weak" attribute; arguments as in | |
4745 | struct attribute_spec.handler. */ | |
4746 | ||
4747 | static tree | |
1cae46be | 4748 | handle_weak_attribute (tree *node, tree name ATTRIBUTE_UNUSED, |
4749 | tree args ATTRIBUTE_UNUSED, | |
4750 | int flags ATTRIBUTE_UNUSED, | |
09347743 | 4751 | bool *no_add_attrs ATTRIBUTE_UNUSED) |
f8e93a2e | 4752 | { |
4753 | declare_weak (*node); | |
4754 | ||
4755 | return NULL_TREE; | |
4756 | } | |
4757 | ||
4758 | /* Handle an "alias" attribute; arguments as in | |
4759 | struct attribute_spec.handler. */ | |
4760 | ||
4761 | static tree | |
1cae46be | 4762 | handle_alias_attribute (tree *node, tree name, tree args, |
09347743 | 4763 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4764 | { |
4765 | tree decl = *node; | |
4766 | ||
4767 | if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) | |
4768 | || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl))) | |
4769 | { | |
f2791601 | 4770 | error ("%H'%D' defined both normally and as an alias", |
4771 | &DECL_SOURCE_LOCATION (decl), decl); | |
f8e93a2e | 4772 | *no_add_attrs = true; |
4773 | } | |
4774 | else if (decl_function_context (decl) == 0) | |
4775 | { | |
4776 | tree id; | |
4777 | ||
4778 | id = TREE_VALUE (args); | |
4779 | if (TREE_CODE (id) != STRING_CST) | |
4780 | { | |
4781 | error ("alias arg not a string"); | |
4782 | *no_add_attrs = true; | |
4783 | return NULL_TREE; | |
4784 | } | |
4785 | id = get_identifier (TREE_STRING_POINTER (id)); | |
4786 | /* This counts as a use of the object pointed to. */ | |
4787 | TREE_USED (id) = 1; | |
4788 | ||
4789 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
4790 | DECL_INITIAL (decl) = error_mark_node; | |
4791 | else | |
4792 | DECL_EXTERNAL (decl) = 0; | |
4793 | } | |
4794 | else | |
4795 | { | |
4796 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4797 | *no_add_attrs = true; | |
4798 | } | |
4799 | ||
4800 | return NULL_TREE; | |
4801 | } | |
4802 | ||
4803 | /* Handle an "visibility" attribute; arguments as in | |
4804 | struct attribute_spec.handler. */ | |
4805 | ||
4806 | static tree | |
1cae46be | 4807 | handle_visibility_attribute (tree *node, tree name, tree args, |
4808 | int flags ATTRIBUTE_UNUSED, | |
09347743 | 4809 | bool *no_add_attrs) |
f8e93a2e | 4810 | { |
4811 | tree decl = *node; | |
4812 | ||
4813 | if (decl_function_context (decl) != 0 || ! TREE_PUBLIC (decl)) | |
4814 | { | |
4815 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4816 | *no_add_attrs = true; | |
4817 | } | |
4818 | else | |
4819 | { | |
4820 | tree id; | |
4821 | ||
4822 | id = TREE_VALUE (args); | |
4823 | if (TREE_CODE (id) != STRING_CST) | |
4824 | { | |
4825 | error ("visibility arg not a string"); | |
4826 | *no_add_attrs = true; | |
4827 | return NULL_TREE; | |
4828 | } | |
4829 | if (strcmp (TREE_STRING_POINTER (id), "hidden") | |
4830 | && strcmp (TREE_STRING_POINTER (id), "protected") | |
8cf34b34 | 4831 | && strcmp (TREE_STRING_POINTER (id), "internal") |
4832 | && strcmp (TREE_STRING_POINTER (id), "default")) | |
f8e93a2e | 4833 | { |
8cf34b34 | 4834 | error ("visibility arg must be one of \"default\", \"hidden\", \"protected\" or \"internal\""); |
f8e93a2e | 4835 | *no_add_attrs = true; |
4836 | return NULL_TREE; | |
4837 | } | |
4838 | } | |
4839 | ||
4840 | return NULL_TREE; | |
4841 | } | |
4842 | ||
24dfead4 | 4843 | /* Handle an "tls_model" attribute; arguments as in |
4844 | struct attribute_spec.handler. */ | |
4845 | ||
4846 | static tree | |
1cae46be | 4847 | handle_tls_model_attribute (tree *node, tree name, tree args, |
09347743 | 4848 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
24dfead4 | 4849 | { |
4850 | tree decl = *node; | |
4851 | ||
4852 | if (! DECL_THREAD_LOCAL (decl)) | |
4853 | { | |
4854 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4855 | *no_add_attrs = true; | |
4856 | } | |
4857 | else | |
4858 | { | |
4859 | tree id; | |
4860 | ||
4861 | id = TREE_VALUE (args); | |
4862 | if (TREE_CODE (id) != STRING_CST) | |
4863 | { | |
4864 | error ("tls_model arg not a string"); | |
4865 | *no_add_attrs = true; | |
4866 | return NULL_TREE; | |
4867 | } | |
4868 | if (strcmp (TREE_STRING_POINTER (id), "local-exec") | |
4869 | && strcmp (TREE_STRING_POINTER (id), "initial-exec") | |
4870 | && strcmp (TREE_STRING_POINTER (id), "local-dynamic") | |
4871 | && strcmp (TREE_STRING_POINTER (id), "global-dynamic")) | |
4872 | { | |
4873 | error ("tls_model arg must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\""); | |
4874 | *no_add_attrs = true; | |
4875 | return NULL_TREE; | |
4876 | } | |
4877 | } | |
4878 | ||
4879 | return NULL_TREE; | |
4880 | } | |
4881 | ||
f8e93a2e | 4882 | /* Handle a "no_instrument_function" attribute; arguments as in |
4883 | struct attribute_spec.handler. */ | |
4884 | ||
4885 | static tree | |
1cae46be | 4886 | handle_no_instrument_function_attribute (tree *node, tree name, |
4887 | tree args ATTRIBUTE_UNUSED, | |
4888 | int flags ATTRIBUTE_UNUSED, | |
09347743 | 4889 | bool *no_add_attrs) |
f8e93a2e | 4890 | { |
4891 | tree decl = *node; | |
4892 | ||
4893 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
4894 | { | |
f2791601 | 4895 | error ("%H'%E' attribute applies only to functions", |
4896 | &DECL_SOURCE_LOCATION (decl), name); | |
f8e93a2e | 4897 | *no_add_attrs = true; |
4898 | } | |
4899 | else if (DECL_INITIAL (decl)) | |
4900 | { | |
f2791601 | 4901 | error ("%Hcan't set '%E' attribute after definition", |
4902 | &DECL_SOURCE_LOCATION (decl), name); | |
f8e93a2e | 4903 | *no_add_attrs = true; |
4904 | } | |
4905 | else | |
4906 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
4907 | ||
4908 | return NULL_TREE; | |
4909 | } | |
4910 | ||
4911 | /* Handle a "malloc" attribute; arguments as in | |
4912 | struct attribute_spec.handler. */ | |
4913 | ||
4914 | static tree | |
1cae46be | 4915 | handle_malloc_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED, |
09347743 | 4916 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4917 | { |
4918 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
4919 | DECL_IS_MALLOC (*node) = 1; | |
4920 | /* ??? TODO: Support types. */ | |
4921 | else | |
4922 | { | |
4923 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4924 | *no_add_attrs = true; | |
4925 | } | |
4926 | ||
4927 | return NULL_TREE; | |
4928 | } | |
4929 | ||
4930 | /* Handle a "no_limit_stack" attribute; arguments as in | |
4931 | struct attribute_spec.handler. */ | |
4932 | ||
4933 | static tree | |
1cae46be | 4934 | handle_no_limit_stack_attribute (tree *node, tree name, |
4935 | tree args ATTRIBUTE_UNUSED, | |
4936 | int flags ATTRIBUTE_UNUSED, | |
09347743 | 4937 | bool *no_add_attrs) |
f8e93a2e | 4938 | { |
4939 | tree decl = *node; | |
4940 | ||
4941 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
4942 | { | |
f2791601 | 4943 | error ("%H'%E' attribute applies only to functions", |
4944 | &DECL_SOURCE_LOCATION (decl), name); | |
f8e93a2e | 4945 | *no_add_attrs = true; |
4946 | } | |
4947 | else if (DECL_INITIAL (decl)) | |
4948 | { | |
f2791601 | 4949 | error ("%Hcan't set '%E' attribute after definition", |
4950 | &DECL_SOURCE_LOCATION (decl), name); | |
f8e93a2e | 4951 | *no_add_attrs = true; |
4952 | } | |
4953 | else | |
4954 | DECL_NO_LIMIT_STACK (decl) = 1; | |
4955 | ||
4956 | return NULL_TREE; | |
4957 | } | |
4958 | ||
4959 | /* Handle a "pure" attribute; arguments as in | |
4960 | struct attribute_spec.handler. */ | |
4961 | ||
4962 | static tree | |
1cae46be | 4963 | handle_pure_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED, |
09347743 | 4964 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
f8e93a2e | 4965 | { |
4966 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
4967 | DECL_IS_PURE (*node) = 1; | |
4968 | /* ??? TODO: Support types. */ | |
4969 | else | |
4970 | { | |
4971 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
4972 | *no_add_attrs = true; | |
4973 | } | |
4974 | ||
4975 | return NULL_TREE; | |
4976 | } | |
4977 | ||
4978 | /* Handle a "deprecated" attribute; arguments as in | |
4979 | struct attribute_spec.handler. */ | |
1cae46be | 4980 | |
f8e93a2e | 4981 | static tree |
1cae46be | 4982 | handle_deprecated_attribute (tree *node, tree name, |
4983 | tree args ATTRIBUTE_UNUSED, int flags, | |
09347743 | 4984 | bool *no_add_attrs) |
f8e93a2e | 4985 | { |
4986 | tree type = NULL_TREE; | |
4987 | int warn = 0; | |
4988 | const char *what = NULL; | |
1cae46be | 4989 | |
f8e93a2e | 4990 | if (DECL_P (*node)) |
4991 | { | |
4992 | tree decl = *node; | |
4993 | type = TREE_TYPE (decl); | |
1cae46be | 4994 | |
f8e93a2e | 4995 | if (TREE_CODE (decl) == TYPE_DECL |
4996 | || TREE_CODE (decl) == PARM_DECL | |
4997 | || TREE_CODE (decl) == VAR_DECL | |
4998 | || TREE_CODE (decl) == FUNCTION_DECL | |
4999 | || TREE_CODE (decl) == FIELD_DECL) | |
5000 | TREE_DEPRECATED (decl) = 1; | |
5001 | else | |
5002 | warn = 1; | |
5003 | } | |
5004 | else if (TYPE_P (*node)) | |
5005 | { | |
5006 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
5007 | *node = build_type_copy (*node); | |
5008 | TREE_DEPRECATED (*node) = 1; | |
5009 | type = *node; | |
5010 | } | |
5011 | else | |
5012 | warn = 1; | |
1cae46be | 5013 | |
f8e93a2e | 5014 | if (warn) |
5015 | { | |
5016 | *no_add_attrs = true; | |
5017 | if (type && TYPE_NAME (type)) | |
5018 | { | |
5019 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
5020 | what = IDENTIFIER_POINTER (TYPE_NAME (*node)); | |
5021 | else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL | |
5022 | && DECL_NAME (TYPE_NAME (type))) | |
5023 | what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))); | |
5024 | } | |
5025 | if (what) | |
5026 | warning ("`%s' attribute ignored for `%s'", | |
5027 | IDENTIFIER_POINTER (name), what); | |
5028 | else | |
1cae46be | 5029 | warning ("`%s' attribute ignored", |
f8e93a2e | 5030 | IDENTIFIER_POINTER (name)); |
5031 | } | |
5032 | ||
5033 | return NULL_TREE; | |
5034 | } | |
5035 | ||
5036 | /* Keep a list of vector type nodes we created in handle_vector_size_attribute, | |
5037 | to prevent us from duplicating type nodes unnecessarily. | |
5038 | The normal mechanism to prevent duplicates is to use type_hash_canon, but | |
5039 | since we want to distinguish types that are essentially identical (except | |
5040 | for their debug representation), we use a local list here. */ | |
bff8c916 | 5041 | static GTY(()) tree vector_type_node_list = 0; |
f8e93a2e | 5042 | |
5043 | /* Handle a "vector_size" attribute; arguments as in | |
5044 | struct attribute_spec.handler. */ | |
5045 | ||
5046 | static tree | |
1cae46be | 5047 | handle_vector_size_attribute (tree *node, tree name, tree args, |
5048 | int flags ATTRIBUTE_UNUSED, | |
09347743 | 5049 | bool *no_add_attrs) |
f8e93a2e | 5050 | { |
5051 | unsigned HOST_WIDE_INT vecsize, nunits; | |
5052 | enum machine_mode mode, orig_mode, new_mode; | |
5053 | tree type = *node, new_type = NULL_TREE; | |
5054 | tree type_list_node; | |
5055 | ||
5056 | *no_add_attrs = true; | |
5057 | ||
5058 | if (! host_integerp (TREE_VALUE (args), 1)) | |
5059 | { | |
5060 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
5061 | return NULL_TREE; | |
5062 | } | |
5063 | ||
5064 | /* Get the vector size (in bytes). */ | |
5065 | vecsize = tree_low_cst (TREE_VALUE (args), 1); | |
5066 | ||
5067 | /* We need to provide for vector pointers, vector arrays, and | |
5068 | functions returning vectors. For example: | |
5069 | ||
5070 | __attribute__((vector_size(16))) short *foo; | |
5071 | ||
5072 | In this case, the mode is SI, but the type being modified is | |
5073 | HI, so we need to look further. */ | |
5074 | ||
5075 | while (POINTER_TYPE_P (type) | |
5076 | || TREE_CODE (type) == FUNCTION_TYPE | |
5077 | || TREE_CODE (type) == ARRAY_TYPE) | |
5078 | type = TREE_TYPE (type); | |
5079 | ||
5080 | /* Get the mode of the type being modified. */ | |
5081 | orig_mode = TYPE_MODE (type); | |
5082 | ||
5083 | if (TREE_CODE (type) == RECORD_TYPE | |
5084 | || (GET_MODE_CLASS (orig_mode) != MODE_FLOAT | |
5085 | && GET_MODE_CLASS (orig_mode) != MODE_INT) | |
5086 | || ! host_integerp (TYPE_SIZE_UNIT (type), 1)) | |
5087 | { | |
5088 | error ("invalid vector type for attribute `%s'", | |
5089 | IDENTIFIER_POINTER (name)); | |
5090 | return NULL_TREE; | |
5091 | } | |
5092 | ||
5093 | /* Calculate how many units fit in the vector. */ | |
5094 | nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1); | |
5095 | ||
5096 | /* Find a suitably sized vector. */ | |
5097 | new_mode = VOIDmode; | |
5098 | for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT | |
5099 | ? MODE_VECTOR_INT | |
5100 | : MODE_VECTOR_FLOAT); | |
5101 | mode != VOIDmode; | |
5102 | mode = GET_MODE_WIDER_MODE (mode)) | |
5103 | if (vecsize == GET_MODE_SIZE (mode) | |
5104 | && nunits == (unsigned HOST_WIDE_INT) GET_MODE_NUNITS (mode)) | |
5105 | { | |
5106 | new_mode = mode; | |
5107 | break; | |
5108 | } | |
5109 | ||
5110 | if (new_mode == VOIDmode) | |
5111 | { | |
5112 | error ("no vector mode with the size and type specified could be found"); | |
5113 | return NULL_TREE; | |
5114 | } | |
5115 | ||
5116 | for (type_list_node = vector_type_node_list; type_list_node; | |
5117 | type_list_node = TREE_CHAIN (type_list_node)) | |
5118 | { | |
5119 | tree other_type = TREE_VALUE (type_list_node); | |
5120 | tree record = TYPE_DEBUG_REPRESENTATION_TYPE (other_type); | |
5121 | tree fields = TYPE_FIELDS (record); | |
5122 | tree field_type = TREE_TYPE (fields); | |
5123 | tree array_type = TREE_TYPE (field_type); | |
5124 | if (TREE_CODE (fields) != FIELD_DECL | |
5125 | || TREE_CODE (field_type) != ARRAY_TYPE) | |
5126 | abort (); | |
5127 | ||
5128 | if (TYPE_MODE (other_type) == mode && type == array_type) | |
5129 | { | |
5130 | new_type = other_type; | |
5131 | break; | |
5132 | } | |
5133 | } | |
5134 | ||
5135 | if (new_type == NULL_TREE) | |
5136 | { | |
5137 | tree index, array, rt, list_node; | |
5138 | ||
5139 | new_type = (*lang_hooks.types.type_for_mode) (new_mode, | |
5140 | TREE_UNSIGNED (type)); | |
5141 | ||
5142 | if (!new_type) | |
5143 | { | |
5144 | error ("no vector mode with the size and type specified could be found"); | |
5145 | return NULL_TREE; | |
5146 | } | |
5147 | ||
5148 | new_type = build_type_copy (new_type); | |
5149 | ||
ead34f59 | 5150 | /* If this is a vector, make sure we either have hardware |
5151 | support, or we can emulate it. */ | |
5152 | if ((GET_MODE_CLASS (mode) == MODE_VECTOR_INT | |
5153 | || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT) | |
5154 | && !vector_mode_valid_p (mode)) | |
5155 | { | |
5156 | error ("unable to emulate '%s'", GET_MODE_NAME (mode)); | |
5157 | return NULL_TREE; | |
5158 | } | |
5159 | ||
f8e93a2e | 5160 | /* Set the debug information here, because this is the only |
5161 | place where we know the underlying type for a vector made | |
5162 | with vector_size. For debugging purposes we pretend a vector | |
5163 | is an array within a structure. */ | |
5164 | index = build_int_2 (TYPE_VECTOR_SUBPARTS (new_type) - 1, 0); | |
5165 | array = build_array_type (type, build_index_type (index)); | |
5166 | rt = make_node (RECORD_TYPE); | |
5167 | ||
5168 | TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array); | |
5169 | DECL_CONTEXT (TYPE_FIELDS (rt)) = rt; | |
5170 | layout_type (rt); | |
5171 | TYPE_DEBUG_REPRESENTATION_TYPE (new_type) = rt; | |
5172 | ||
5173 | list_node = build_tree_list (NULL, new_type); | |
5174 | TREE_CHAIN (list_node) = vector_type_node_list; | |
5175 | vector_type_node_list = list_node; | |
5176 | } | |
5177 | ||
5178 | /* Build back pointers if needed. */ | |
5179 | *node = vector_size_helper (*node, new_type); | |
5180 | ||
5181 | return NULL_TREE; | |
5182 | } | |
5183 | ||
5184 | /* HACK. GROSS. This is absolutely disgusting. I wish there was a | |
5185 | better way. | |
5186 | ||
5187 | If we requested a pointer to a vector, build up the pointers that | |
5188 | we stripped off while looking for the inner type. Similarly for | |
5189 | return values from functions. | |
5190 | ||
5191 | The argument "type" is the top of the chain, and "bottom" is the | |
5192 | new type which we will point to. */ | |
5193 | ||
5194 | static tree | |
1cae46be | 5195 | vector_size_helper (tree type, tree bottom) |
f8e93a2e | 5196 | { |
5197 | tree inner, outer; | |
5198 | ||
5199 | if (POINTER_TYPE_P (type)) | |
5200 | { | |
5201 | inner = vector_size_helper (TREE_TYPE (type), bottom); | |
5202 | outer = build_pointer_type (inner); | |
5203 | } | |
5204 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
5205 | { | |
5206 | inner = vector_size_helper (TREE_TYPE (type), bottom); | |
5207 | outer = build_array_type (inner, TYPE_VALUES (type)); | |
5208 | } | |
5209 | else if (TREE_CODE (type) == FUNCTION_TYPE) | |
5210 | { | |
5211 | inner = vector_size_helper (TREE_TYPE (type), bottom); | |
5212 | outer = build_function_type (inner, TYPE_VALUES (type)); | |
5213 | } | |
5214 | else | |
5215 | return bottom; | |
1cae46be | 5216 | |
f8e93a2e | 5217 | TREE_READONLY (outer) = TREE_READONLY (type); |
5218 | TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type); | |
5219 | ||
5220 | return outer; | |
5221 | } | |
dbf6c367 | 5222 | |
5223 | /* Handle the "nonnull" attribute. */ | |
5224 | static tree | |
1cae46be | 5225 | handle_nonnull_attribute (tree *node, tree name ATTRIBUTE_UNUSED, |
5226 | tree args, int flags ATTRIBUTE_UNUSED, | |
09347743 | 5227 | bool *no_add_attrs) |
dbf6c367 | 5228 | { |
5229 | tree type = *node; | |
5230 | unsigned HOST_WIDE_INT attr_arg_num; | |
5231 | ||
5232 | /* If no arguments are specified, all pointer arguments should be | |
d716ce75 | 5233 | non-null. Verify a full prototype is given so that the arguments |
dbf6c367 | 5234 | will have the correct types when we actually check them later. */ |
5235 | if (! args) | |
5236 | { | |
5237 | if (! TYPE_ARG_TYPES (type)) | |
5238 | { | |
5239 | error ("nonnull attribute without arguments on a non-prototype"); | |
5240 | *no_add_attrs = true; | |
5241 | } | |
5242 | return NULL_TREE; | |
5243 | } | |
5244 | ||
5245 | /* Argument list specified. Verify that each argument number references | |
5246 | a pointer argument. */ | |
5247 | for (attr_arg_num = 1; args; args = TREE_CHAIN (args)) | |
5248 | { | |
5249 | tree argument; | |
5250 | unsigned HOST_WIDE_INT arg_num, ck_num; | |
5251 | ||
5252 | if (! get_nonnull_operand (TREE_VALUE (args), &arg_num)) | |
5253 | { | |
5254 | error ("nonnull argument has invalid operand number (arg %lu)", | |
5255 | (unsigned long) attr_arg_num); | |
5256 | *no_add_attrs = true; | |
5257 | return NULL_TREE; | |
5258 | } | |
5259 | ||
5260 | argument = TYPE_ARG_TYPES (type); | |
5261 | if (argument) | |
5262 | { | |
5263 | for (ck_num = 1; ; ck_num++) | |
5264 | { | |
5265 | if (! argument || ck_num == arg_num) | |
5266 | break; | |
5267 | argument = TREE_CHAIN (argument); | |
5268 | } | |
5269 | ||
5270 | if (! argument | |
5271 | || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE) | |
5272 | { | |
5273 | error ("nonnull argument with out-of-range operand number (arg %lu, operand %lu)", | |
5274 | (unsigned long) attr_arg_num, (unsigned long) arg_num); | |
5275 | *no_add_attrs = true; | |
5276 | return NULL_TREE; | |
5277 | } | |
5278 | ||
5279 | if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE) | |
5280 | { | |
5281 | error ("nonnull argument references non-pointer operand (arg %lu, operand %lu)", | |
5282 | (unsigned long) attr_arg_num, (unsigned long) arg_num); | |
5283 | *no_add_attrs = true; | |
5284 | return NULL_TREE; | |
5285 | } | |
5286 | } | |
5287 | } | |
5288 | ||
5289 | return NULL_TREE; | |
5290 | } | |
5291 | ||
5292 | /* Check the argument list of a function call for null in argument slots | |
5293 | that are marked as requiring a non-null pointer argument. */ | |
5294 | ||
5295 | static void | |
1cae46be | 5296 | check_function_nonnull (tree attrs, tree params) |
dbf6c367 | 5297 | { |
5298 | tree a, args, param; | |
5299 | int param_num; | |
5300 | ||
5301 | for (a = attrs; a; a = TREE_CHAIN (a)) | |
5302 | { | |
5303 | if (is_attribute_p ("nonnull", TREE_PURPOSE (a))) | |
5304 | { | |
5305 | args = TREE_VALUE (a); | |
5306 | ||
5307 | /* Walk the argument list. If we encounter an argument number we | |
5308 | should check for non-null, do it. If the attribute has no args, | |
5309 | then every pointer argument is checked (in which case the check | |
5310 | for pointer type is done in check_nonnull_arg). */ | |
5311 | for (param = params, param_num = 1; ; | |
5312 | param_num++, param = TREE_CHAIN (param)) | |
5313 | { | |
5314 | if (! param) | |
1cae46be | 5315 | break; |
dbf6c367 | 5316 | if (! args || nonnull_check_p (args, param_num)) |
1cae46be | 5317 | check_function_arguments_recurse (check_nonnull_arg, NULL, |
5318 | TREE_VALUE (param), | |
5319 | param_num); | |
dbf6c367 | 5320 | } |
5321 | } | |
5322 | } | |
5323 | } | |
5324 | ||
5325 | /* Helper for check_function_nonnull; given a list of operands which | |
5326 | must be non-null in ARGS, determine if operand PARAM_NUM should be | |
5327 | checked. */ | |
5328 | ||
5329 | static bool | |
1cae46be | 5330 | nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 5331 | { |
5332 | unsigned HOST_WIDE_INT arg_num; | |
5333 | ||
5334 | for (; args; args = TREE_CHAIN (args)) | |
5335 | { | |
5336 | if (! get_nonnull_operand (TREE_VALUE (args), &arg_num)) | |
5337 | abort (); | |
5338 | ||
5339 | if (arg_num == param_num) | |
5340 | return true; | |
5341 | } | |
5342 | return false; | |
5343 | } | |
5344 | ||
5345 | /* Check that the function argument PARAM (which is operand number | |
5346 | PARAM_NUM) is non-null. This is called by check_function_nonnull | |
5347 | via check_function_arguments_recurse. */ | |
5348 | ||
5349 | static void | |
1cae46be | 5350 | check_nonnull_arg (void *ctx ATTRIBUTE_UNUSED, tree param, |
5351 | unsigned HOST_WIDE_INT param_num) | |
dbf6c367 | 5352 | { |
5353 | /* Just skip checking the argument if it's not a pointer. This can | |
5354 | happen if the "nonnull" attribute was given without an operand | |
5355 | list (which means to check every pointer argument). */ | |
5356 | ||
5357 | if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) | |
5358 | return; | |
5359 | ||
5360 | if (integer_zerop (param)) | |
5361 | warning ("null argument where non-null required (arg %lu)", | |
5362 | (unsigned long) param_num); | |
5363 | } | |
5364 | ||
5365 | /* Helper for nonnull attribute handling; fetch the operand number | |
5366 | from the attribute argument list. */ | |
5367 | ||
5368 | static bool | |
1cae46be | 5369 | get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) |
dbf6c367 | 5370 | { |
5371 | /* Strip any conversions from the arg number and verify they | |
5372 | are constants. */ | |
5373 | while (TREE_CODE (arg_num_expr) == NOP_EXPR | |
5374 | || TREE_CODE (arg_num_expr) == CONVERT_EXPR | |
5375 | || TREE_CODE (arg_num_expr) == NON_LVALUE_EXPR) | |
5376 | arg_num_expr = TREE_OPERAND (arg_num_expr, 0); | |
5377 | ||
5378 | if (TREE_CODE (arg_num_expr) != INTEGER_CST | |
5379 | || TREE_INT_CST_HIGH (arg_num_expr) != 0) | |
5380 | return false; | |
5381 | ||
5382 | *valp = TREE_INT_CST_LOW (arg_num_expr); | |
5383 | return true; | |
5384 | } | |
fa987697 | 5385 | |
5386 | /* Handle a "nothrow" attribute; arguments as in | |
5387 | struct attribute_spec.handler. */ | |
5388 | ||
5389 | static tree | |
1cae46be | 5390 | handle_nothrow_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED, |
09347743 | 5391 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
fa987697 | 5392 | { |
5393 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5394 | TREE_NOTHROW (*node) = 1; | |
5395 | /* ??? TODO: Support types. */ | |
5396 | else | |
5397 | { | |
5398 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
5399 | *no_add_attrs = true; | |
5400 | } | |
5401 | ||
5402 | return NULL_TREE; | |
5403 | } | |
7acb29a3 | 5404 | |
5405 | /* Handle a "cleanup" attribute; arguments as in | |
5406 | struct attribute_spec.handler. */ | |
5407 | ||
5408 | static tree | |
1cae46be | 5409 | handle_cleanup_attribute (tree *node, tree name, tree args, |
09347743 | 5410 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) |
7acb29a3 | 5411 | { |
5412 | tree decl = *node; | |
5413 | tree cleanup_id, cleanup_decl; | |
5414 | ||
5415 | /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do | |
5416 | for global destructors in C++. This requires infrastructure that | |
5417 | we don't have generically at the moment. It's also not a feature | |
5418 | we'd be missing too much, since we do have attribute constructor. */ | |
5419 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
5420 | { | |
5421 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
5422 | *no_add_attrs = true; | |
5423 | return NULL_TREE; | |
5424 | } | |
5425 | ||
5426 | /* Verify that the argument is a function in scope. */ | |
5427 | /* ??? We could support pointers to functions here as well, if | |
5428 | that was considered desirable. */ | |
5429 | cleanup_id = TREE_VALUE (args); | |
5430 | if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE) | |
5431 | { | |
5432 | error ("cleanup arg not an identifier"); | |
5433 | *no_add_attrs = true; | |
5434 | return NULL_TREE; | |
5435 | } | |
5436 | cleanup_decl = lookup_name (cleanup_id); | |
5437 | if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL) | |
5438 | { | |
5439 | error ("cleanup arg not a function"); | |
5440 | *no_add_attrs = true; | |
5441 | return NULL_TREE; | |
5442 | } | |
5443 | ||
1cae46be | 5444 | /* That the function has proper type is checked with the |
7acb29a3 | 5445 | eventual call to build_function_call. */ |
5446 | ||
5447 | return NULL_TREE; | |
5448 | } | |
dbf6c367 | 5449 | \f |
5450 | /* Check for valid arguments being passed to a function. */ | |
5451 | void | |
1cae46be | 5452 | check_function_arguments (tree attrs, tree params) |
dbf6c367 | 5453 | { |
5454 | /* Check for null being passed in a pointer argument that must be | |
5455 | non-null. We also need to do this if format checking is enabled. */ | |
5456 | ||
5457 | if (warn_nonnull) | |
5458 | check_function_nonnull (attrs, params); | |
5459 | ||
5460 | /* Check for errors in format strings. */ | |
5461 | ||
5462 | if (warn_format) | |
5463 | check_function_format (NULL, attrs, params); | |
5464 | } | |
5465 | ||
5466 | /* Generic argument checking recursion routine. PARAM is the argument to | |
5467 | be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked | |
5468 | once the argument is resolved. CTX is context for the callback. */ | |
5469 | void | |
1cae46be | 5470 | check_function_arguments_recurse (void (*callback) |
5471 | (void *, tree, unsigned HOST_WIDE_INT), | |
5472 | void *ctx, tree param, | |
5473 | unsigned HOST_WIDE_INT param_num) | |
dbf6c367 | 5474 | { |
5475 | if (TREE_CODE (param) == NOP_EXPR) | |
5476 | { | |
5477 | /* Strip coercion. */ | |
5478 | check_function_arguments_recurse (callback, ctx, | |
5479 | TREE_OPERAND (param, 0), param_num); | |
5480 | return; | |
5481 | } | |
5482 | ||
5483 | if (TREE_CODE (param) == CALL_EXPR) | |
5484 | { | |
5485 | tree type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (param, 0))); | |
5486 | tree attrs; | |
5487 | bool found_format_arg = false; | |
5488 | ||
5489 | /* See if this is a call to a known internationalization function | |
5490 | that modifies a format arg. Such a function may have multiple | |
5491 | format_arg attributes (for example, ngettext). */ | |
5492 | ||
5493 | for (attrs = TYPE_ATTRIBUTES (type); | |
5494 | attrs; | |
5495 | attrs = TREE_CHAIN (attrs)) | |
5496 | if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs))) | |
5497 | { | |
5498 | tree inner_args; | |
5499 | tree format_num_expr; | |
5500 | int format_num; | |
5501 | int i; | |
5502 | ||
5503 | /* Extract the argument number, which was previously checked | |
5504 | to be valid. */ | |
5505 | format_num_expr = TREE_VALUE (TREE_VALUE (attrs)); | |
5506 | while (TREE_CODE (format_num_expr) == NOP_EXPR | |
5507 | || TREE_CODE (format_num_expr) == CONVERT_EXPR | |
5508 | || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR) | |
5509 | format_num_expr = TREE_OPERAND (format_num_expr, 0); | |
5510 | ||
5511 | if (TREE_CODE (format_num_expr) != INTEGER_CST | |
5512 | || TREE_INT_CST_HIGH (format_num_expr) != 0) | |
5513 | abort (); | |
5514 | ||
5515 | format_num = TREE_INT_CST_LOW (format_num_expr); | |
5516 | ||
5517 | for (inner_args = TREE_OPERAND (param, 1), i = 1; | |
5518 | inner_args != 0; | |
5519 | inner_args = TREE_CHAIN (inner_args), i++) | |
5520 | if (i == format_num) | |
5521 | { | |
5522 | check_function_arguments_recurse (callback, ctx, | |
5523 | TREE_VALUE (inner_args), | |
5524 | param_num); | |
5525 | found_format_arg = true; | |
5526 | break; | |
5527 | } | |
5528 | } | |
5529 | ||
5530 | /* If we found a format_arg attribute and did a recursive check, | |
5531 | we are done with checking this argument. Otherwise, we continue | |
5532 | and this will be considered a non-literal. */ | |
5533 | if (found_format_arg) | |
5534 | return; | |
5535 | } | |
5536 | ||
5537 | if (TREE_CODE (param) == COND_EXPR) | |
5538 | { | |
5539 | /* Check both halves of the conditional expression. */ | |
5540 | check_function_arguments_recurse (callback, ctx, | |
5541 | TREE_OPERAND (param, 1), param_num); | |
5542 | check_function_arguments_recurse (callback, ctx, | |
5543 | TREE_OPERAND (param, 2), param_num); | |
5544 | return; | |
5545 | } | |
5546 | ||
5547 | (*callback) (ctx, param, param_num); | |
5548 | } | |
1f3233d1 | 5549 | |
860251be | 5550 | /* Function to help qsort sort FIELD_DECLs by name order. */ |
5551 | ||
5552 | int | |
5553 | field_decl_cmp (const void *x_p, const void *y_p) | |
5554 | { | |
5555 | const tree *const x = x_p; | |
5556 | const tree *const y = y_p; | |
5557 | if (DECL_NAME (*x) == DECL_NAME (*y)) | |
5558 | /* A nontype is "greater" than a type. */ | |
5559 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
5560 | if (DECL_NAME (*x) == NULL_TREE) | |
5561 | return -1; | |
5562 | if (DECL_NAME (*y) == NULL_TREE) | |
5563 | return 1; | |
5564 | if (DECL_NAME (*x) < DECL_NAME (*y)) | |
5565 | return -1; | |
5566 | return 1; | |
5567 | } | |
5568 | ||
5569 | static struct { | |
5570 | gt_pointer_operator new_value; | |
5571 | void *cookie; | |
5572 | } resort_data; | |
5573 | ||
5574 | /* This routine compares two fields like field_decl_cmp but using the | |
5575 | pointer operator in resort_data. */ | |
5576 | ||
5577 | static int | |
5578 | resort_field_decl_cmp (const void *x_p, const void *y_p) | |
5579 | { | |
5580 | const tree *const x = x_p; | |
5581 | const tree *const y = y_p; | |
5582 | ||
5583 | if (DECL_NAME (*x) == DECL_NAME (*y)) | |
5584 | /* A nontype is "greater" than a type. */ | |
5585 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
5586 | if (DECL_NAME (*x) == NULL_TREE) | |
5587 | return -1; | |
5588 | if (DECL_NAME (*y) == NULL_TREE) | |
5589 | return 1; | |
5590 | { | |
5591 | tree d1 = DECL_NAME (*x); | |
5592 | tree d2 = DECL_NAME (*y); | |
5593 | resort_data.new_value (&d1, resort_data.cookie); | |
5594 | resort_data.new_value (&d2, resort_data.cookie); | |
5595 | if (d1 < d2) | |
5596 | return -1; | |
5597 | } | |
5598 | return 1; | |
5599 | } | |
5600 | ||
5601 | /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ | |
5602 | ||
5603 | void | |
5604 | resort_sorted_fields (void *obj, | |
5605 | void *orig_obj ATTRIBUTE_UNUSED , | |
5606 | gt_pointer_operator new_value, | |
5607 | void *cookie) | |
5608 | { | |
5609 | struct sorted_fields_type *sf = obj; | |
5610 | resort_data.new_value = new_value; | |
5611 | resort_data.cookie = cookie; | |
5612 | qsort (&sf->elts[0], sf->len, sizeof (tree), | |
5613 | resort_field_decl_cmp); | |
5614 | } | |
5615 | ||
309c7c52 | 5616 | /* Used by estimate_num_insns. Estimate number of instructions seen |
5617 | by given statement. */ | |
5618 | static tree | |
5619 | c_estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data) | |
5620 | { | |
5621 | int *count = data; | |
5622 | tree x = *tp; | |
5623 | ||
5624 | if (TYPE_P (x) || DECL_P (x)) | |
5625 | { | |
5626 | *walk_subtrees = 0; | |
5627 | return NULL; | |
5628 | } | |
5629 | /* Assume that constants and references counts nothing. These should | |
5630 | be majorized by amount of operations amoung them we count later | |
5631 | and are common target of CSE and similar optimizations. */ | |
5632 | if (TREE_CODE_CLASS (TREE_CODE (x)) == 'c' | |
5633 | || TREE_CODE_CLASS (TREE_CODE (x)) == 'r') | |
5634 | return NULL; | |
5635 | switch (TREE_CODE (x)) | |
5636 | { | |
5637 | /* Reconginze assignments of large structures and constructors of | |
5638 | big arrays. */ | |
5639 | case MODIFY_EXPR: | |
5640 | case CONSTRUCTOR: | |
5641 | { | |
5642 | int size = int_size_in_bytes (TREE_TYPE (x)); | |
5643 | ||
5644 | if (!size || size > MOVE_MAX_PIECES) | |
5645 | *count += 10; | |
5646 | else | |
5647 | *count += 2 * (size + MOVE_MAX - 1) / MOVE_MAX; | |
5648 | return NULL; | |
5649 | } | |
5650 | break; | |
5651 | /* Few special cases of expensive operations. This is usefull | |
5652 | to avoid inlining on functions having too many of these. */ | |
5653 | case TRUNC_DIV_EXPR: | |
5654 | case CEIL_DIV_EXPR: | |
5655 | case FLOOR_DIV_EXPR: | |
5656 | case ROUND_DIV_EXPR: | |
5657 | case TRUNC_MOD_EXPR: | |
5658 | case CEIL_MOD_EXPR: | |
5659 | case FLOOR_MOD_EXPR: | |
5660 | case ROUND_MOD_EXPR: | |
5661 | case RDIV_EXPR: | |
5662 | case CALL_EXPR: | |
309c7c52 | 5663 | *count += 10; |
5664 | break; | |
5665 | /* Various containers that will produce no code themselves. */ | |
5666 | case INIT_EXPR: | |
5667 | case TARGET_EXPR: | |
5668 | case BIND_EXPR: | |
5669 | case BLOCK: | |
5670 | case TREE_LIST: | |
5671 | case TREE_VEC: | |
5672 | case IDENTIFIER_NODE: | |
5673 | case PLACEHOLDER_EXPR: | |
5674 | case WITH_CLEANUP_EXPR: | |
5675 | case CLEANUP_POINT_EXPR: | |
5676 | case NOP_EXPR: | |
5677 | case VIEW_CONVERT_EXPR: | |
5678 | case SAVE_EXPR: | |
5679 | case UNSAVE_EXPR: | |
5680 | case COMPLEX_EXPR: | |
5681 | case REALPART_EXPR: | |
5682 | case IMAGPART_EXPR: | |
5683 | case TRY_CATCH_EXPR: | |
5684 | case TRY_FINALLY_EXPR: | |
5685 | case LABEL_EXPR: | |
5686 | case EXIT_EXPR: | |
5687 | case LABELED_BLOCK_EXPR: | |
5688 | case EXIT_BLOCK_EXPR: | |
5689 | case EXPR_WITH_FILE_LOCATION: | |
5690 | ||
5691 | case EXPR_STMT: | |
5692 | case COMPOUND_STMT: | |
5693 | case RETURN_STMT: | |
5694 | case LABEL_STMT: | |
5695 | case SCOPE_STMT: | |
5696 | case FILE_STMT: | |
5697 | case CASE_LABEL: | |
5698 | case STMT_EXPR: | |
5699 | case CLEANUP_STMT: | |
5700 | ||
5701 | case SIZEOF_EXPR: | |
5702 | case ARROW_EXPR: | |
5703 | case ALIGNOF_EXPR: | |
5704 | break; | |
5705 | case DECL_STMT: | |
5706 | /* Do not account static initializers. */ | |
5707 | if (TREE_STATIC (TREE_OPERAND (x, 0))) | |
5708 | *walk_subtrees = 0; | |
5709 | break; | |
5710 | default: | |
5711 | (*count)++; | |
5712 | } | |
5713 | return NULL; | |
5714 | } | |
5715 | ||
5716 | /* Estimate number of instructions that will be created by expanding the body. */ | |
5717 | int | |
5718 | c_estimate_num_insns (tree decl) | |
5719 | { | |
5720 | int num = 0; | |
5721 | walk_tree_without_duplicates (&DECL_SAVED_TREE (decl), c_estimate_num_insns_1, &num); | |
5722 | return num; | |
5723 | } | |
5724 | ||
1f3233d1 | 5725 | #include "gt-c-common.h" |