]>
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, |
78c2e180 | 3 | 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 | |
8c4c00c1 | 9 | Software Foundation; either version 3, or (at your option) any later |
f12b58b3 | 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 | |
8c4c00c1 | 18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
b0fc3e72 | 20 | |
21 | #include "config.h" | |
405711de | 22 | #include "system.h" |
805e22b2 | 23 | #include "coretypes.h" |
24 | #include "tm.h" | |
e48d0f41 | 25 | #include "intl.h" |
b0fc3e72 | 26 | #include "tree.h" |
b0fc3e72 | 27 | #include "flags.h" |
cd03a192 | 28 | #include "output.h" |
a3fa7feb | 29 | #include "c-pragma.h" |
a5b1863e | 30 | #include "rtl.h" |
dc12af01 | 31 | #include "ggc.h" |
573aba85 | 32 | #include "varray.h" |
74647769 | 33 | #include "expr.h" |
e41f0d80 | 34 | #include "c-common.h" |
cdc9fa3e | 35 | #include "diagnostic.h" |
d8c9779c | 36 | #include "tm_p.h" |
4e91a871 | 37 | #include "obstack.h" |
a654e028 | 38 | #include "cpplib.h" |
8ee295a7 | 39 | #include "target.h" |
96554925 | 40 | #include "langhooks.h" |
f3dde807 | 41 | #include "tree-inline.h" |
7acb29a3 | 42 | #include "c-tree.h" |
69579044 | 43 | #include "toplev.h" |
4ee9c684 | 44 | #include "tree-iterator.h" |
45 | #include "hashtab.h" | |
b55af61c | 46 | #include "tree-mudflap.h" |
e08bd2f4 | 47 | #include "opts.h" |
5000e21c | 48 | #include "real.h" |
62eec3b4 | 49 | #include "cgraph.h" |
b9fc964a | 50 | #include "target-def.h" |
9421ebb9 | 51 | #include "fixed-value.h" |
fd6f6435 | 52 | |
90cc7820 | 53 | cpp_reader *parse_in; /* Declared in c-pragma.h. */ |
a654e028 | 54 | |
174fcc61 | 55 | /* We let tm.h override the types used here, to handle trivial differences |
56 | such as the choice of unsigned int or long unsigned int for size_t. | |
57 | When machines start needing nontrivial differences in the size type, | |
58 | it would be best to do something here to figure out automatically | |
59 | from other information what type to use. */ | |
60 | ||
61 | #ifndef SIZE_TYPE | |
62 | #define SIZE_TYPE "long unsigned int" | |
63 | #endif | |
64 | ||
73673831 | 65 | #ifndef PID_TYPE |
66 | #define PID_TYPE "int" | |
67 | #endif | |
68 | ||
924bbf02 | 69 | #ifndef CHAR16_TYPE |
70 | #define CHAR16_TYPE "short unsigned int" | |
71 | #endif | |
72 | ||
73 | #ifndef CHAR32_TYPE | |
74 | #define CHAR32_TYPE "unsigned int" | |
75 | #endif | |
76 | ||
174fcc61 | 77 | #ifndef WCHAR_TYPE |
78 | #define WCHAR_TYPE "int" | |
79 | #endif | |
80 | ||
18ef7ac2 | 81 | /* WCHAR_TYPE gets overridden by -fshort-wchar. */ |
82 | #define MODIFIED_WCHAR_TYPE \ | |
83 | (flag_short_wchar ? "short unsigned int" : WCHAR_TYPE) | |
84 | ||
194c4d9f | 85 | #ifndef PTRDIFF_TYPE |
86 | #define PTRDIFF_TYPE "long int" | |
87 | #endif | |
88 | ||
6bf5ed8d | 89 | #ifndef WINT_TYPE |
90 | #define WINT_TYPE "unsigned int" | |
91 | #endif | |
92 | ||
93 | #ifndef INTMAX_TYPE | |
94 | #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
95 | ? "int" \ | |
96 | : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
97 | ? "long int" \ | |
98 | : "long long int")) | |
99 | #endif | |
100 | ||
101 | #ifndef UINTMAX_TYPE | |
102 | #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
103 | ? "unsigned int" \ | |
104 | : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
105 | ? "long unsigned int" \ | |
106 | : "long long unsigned int")) | |
107 | #endif | |
108 | ||
72040e7e | 109 | /* The following symbols are subsumed in the c_global_trees array, and |
44e9fa65 | 110 | listed here individually for documentation purposes. |
72040e7e | 111 | |
112 | INTEGER_TYPE and REAL_TYPE nodes for the standard data types. | |
113 | ||
114 | tree short_integer_type_node; | |
115 | tree long_integer_type_node; | |
116 | tree long_long_integer_type_node; | |
117 | ||
118 | tree short_unsigned_type_node; | |
119 | tree long_unsigned_type_node; | |
120 | tree long_long_unsigned_type_node; | |
121 | ||
3c2239cf | 122 | tree truthvalue_type_node; |
123 | tree truthvalue_false_node; | |
124 | tree truthvalue_true_node; | |
72040e7e | 125 | |
126 | tree ptrdiff_type_node; | |
127 | ||
128 | tree unsigned_char_type_node; | |
129 | tree signed_char_type_node; | |
130 | tree wchar_type_node; | |
131 | tree signed_wchar_type_node; | |
132 | tree unsigned_wchar_type_node; | |
133 | ||
924bbf02 | 134 | tree char16_type_node; |
135 | tree char32_type_node; | |
136 | ||
72040e7e | 137 | tree float_type_node; |
138 | tree double_type_node; | |
139 | tree long_double_type_node; | |
140 | ||
141 | tree complex_integer_type_node; | |
142 | tree complex_float_type_node; | |
143 | tree complex_double_type_node; | |
144 | tree complex_long_double_type_node; | |
145 | ||
c4503c0a | 146 | tree dfloat32_type_node; |
147 | tree dfloat64_type_node; | |
148 | tree_dfloat128_type_node; | |
149 | ||
72040e7e | 150 | tree intQI_type_node; |
151 | tree intHI_type_node; | |
152 | tree intSI_type_node; | |
153 | tree intDI_type_node; | |
154 | tree intTI_type_node; | |
155 | ||
156 | tree unsigned_intQI_type_node; | |
157 | tree unsigned_intHI_type_node; | |
158 | tree unsigned_intSI_type_node; | |
159 | tree unsigned_intDI_type_node; | |
160 | tree unsigned_intTI_type_node; | |
161 | ||
162 | tree widest_integer_literal_type_node; | |
163 | tree widest_unsigned_literal_type_node; | |
164 | ||
165 | Nodes for types `void *' and `const void *'. | |
166 | ||
167 | tree ptr_type_node, const_ptr_type_node; | |
168 | ||
169 | Nodes for types `char *' and `const char *'. | |
170 | ||
171 | tree string_type_node, const_string_type_node; | |
172 | ||
173 | Type `char[SOMENUMBER]'. | |
174 | Used when an array of char is needed and the size is irrelevant. | |
175 | ||
176 | tree char_array_type_node; | |
177 | ||
178 | Type `int[SOMENUMBER]' or something like it. | |
179 | Used when an array of int needed and the size is irrelevant. | |
180 | ||
181 | tree int_array_type_node; | |
182 | ||
183 | Type `wchar_t[SOMENUMBER]' or something like it. | |
184 | Used when a wide string literal is created. | |
185 | ||
186 | tree wchar_array_type_node; | |
187 | ||
924bbf02 | 188 | Type `char16_t[SOMENUMBER]' or something like it. |
189 | Used when a UTF-16 string literal is created. | |
190 | ||
191 | tree char16_array_type_node; | |
192 | ||
193 | Type `char32_t[SOMENUMBER]' or something like it. | |
194 | Used when a UTF-32 string literal is created. | |
195 | ||
196 | tree char32_array_type_node; | |
197 | ||
72040e7e | 198 | Type `int ()' -- used for implicit declaration of functions. |
199 | ||
200 | tree default_function_type; | |
201 | ||
72040e7e | 202 | A VOID_TYPE node, packaged in a TREE_LIST. |
203 | ||
204 | tree void_list_node; | |
205 | ||
734c98be | 206 | The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__, |
65b7f83f | 207 | and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__ |
208 | VAR_DECLS, but C++ does.) | |
71d9fc9b | 209 | |
65b7f83f | 210 | tree function_name_decl_node; |
734c98be | 211 | tree pretty_function_name_decl_node; |
65b7f83f | 212 | tree c99_function_name_decl_node; |
213 | ||
214 | Stack of nested function name VAR_DECLs. | |
1cae46be | 215 | |
65b7f83f | 216 | tree saved_function_name_decls; |
71d9fc9b | 217 | |
72040e7e | 218 | */ |
219 | ||
220 | tree c_global_trees[CTI_MAX]; | |
573aba85 | 221 | \f |
574a6990 | 222 | /* Switches common to the C front ends. */ |
223 | ||
f0b5f617 | 224 | /* Nonzero if preprocessing only. */ |
f7070933 | 225 | |
ffd56b21 | 226 | int flag_preprocess_only; |
227 | ||
f7070933 | 228 | /* Nonzero means don't output line number information. */ |
229 | ||
230 | char flag_no_line_commands; | |
231 | ||
232 | /* Nonzero causes -E output not to be done, but directives such as | |
233 | #define that have side effects are still obeyed. */ | |
234 | ||
235 | char flag_no_output; | |
236 | ||
237 | /* Nonzero means dump macros in some fashion. */ | |
238 | ||
239 | char flag_dump_macros; | |
240 | ||
241 | /* Nonzero means pass #include lines through to the output. */ | |
242 | ||
243 | char flag_dump_includes; | |
244 | ||
d718b525 | 245 | /* Nonzero means process PCH files while preprocessing. */ |
246 | ||
247 | bool flag_pch_preprocess; | |
248 | ||
573aba85 | 249 | /* The file name to which we should write a precompiled header, or |
250 | NULL if no header will be written in this compile. */ | |
251 | ||
252 | const char *pch_file; | |
253 | ||
1ed9d5f5 | 254 | /* Nonzero if an ISO standard was selected. It rejects macros in the |
255 | user's namespace. */ | |
256 | int flag_iso; | |
257 | ||
258 | /* Nonzero if -undef was given. It suppresses target built-in macros | |
259 | and assertions. */ | |
260 | int flag_undef; | |
261 | ||
0270ae90 | 262 | /* Nonzero means don't recognize the non-ANSI builtin functions. */ |
263 | ||
264 | int flag_no_builtin; | |
265 | ||
266 | /* Nonzero means don't recognize the non-ANSI builtin functions. | |
267 | -ansi sets this. */ | |
268 | ||
269 | int flag_no_nonansi_builtin; | |
270 | ||
174fcc61 | 271 | /* Nonzero means give `double' the same size as `float'. */ |
272 | ||
273 | int flag_short_double; | |
274 | ||
275 | /* Nonzero means give `wchar_t' the same size as `short'. */ | |
276 | ||
277 | int flag_short_wchar; | |
278 | ||
546c4794 | 279 | /* Nonzero means allow implicit conversions between vectors with |
280 | differing numbers of subparts and/or differing element types. */ | |
281 | int flag_lax_vector_conversions; | |
282 | ||
07ebd091 | 283 | /* Nonzero means allow Microsoft extensions without warnings or errors. */ |
284 | int flag_ms_extensions; | |
285 | ||
574a6990 | 286 | /* Nonzero means don't recognize the keyword `asm'. */ |
287 | ||
288 | int flag_no_asm; | |
289 | ||
574a6990 | 290 | /* Nonzero means to treat bitfields as signed unless they say `unsigned'. */ |
291 | ||
292 | int flag_signed_bitfields = 1; | |
574a6990 | 293 | |
1cae46be | 294 | /* Warn about #pragma directives that are not recognized. */ |
574a6990 | 295 | |
1cae46be | 296 | int warn_unknown_pragmas; /* Tri state variable. */ |
574a6990 | 297 | |
574a6990 | 298 | /* Warn about format/argument anomalies in calls to formatted I/O functions |
299 | (*printf, *scanf, strftime, strfmon, etc.). */ | |
300 | ||
301 | int warn_format; | |
302 | ||
ae84079f | 303 | /* Warn about using __null (as NULL in C++) as sentinel. For code compiled |
304 | with GCC this doesn't matter as __null is guaranteed to have the right | |
305 | size. */ | |
306 | ||
307 | int warn_strict_null_sentinel; | |
308 | ||
c17b85ea | 309 | /* Zero means that faster, ...NonNil variants of objc_msgSend... |
310 | calls will be used in ObjC; passing nil receivers to such calls | |
311 | will most likely result in crashes. */ | |
312 | int flag_nil_receivers = 1; | |
313 | ||
c17b85ea | 314 | /* Nonzero means that code generation will be altered to support |
315 | "zero-link" execution. This currently affects ObjC only, but may | |
316 | affect other languages in the future. */ | |
317 | int flag_zero_link = 0; | |
318 | ||
319 | /* Nonzero means emit an '__OBJC, __image_info' for the current translation | |
320 | unit. It will inform the ObjC runtime that class definition(s) herein | |
321 | contained are to replace one(s) previously loaded. */ | |
322 | int flag_replace_objc_classes = 0; | |
b27ac6b5 | 323 | |
574a6990 | 324 | /* C/ObjC language option variables. */ |
325 | ||
326 | ||
574a6990 | 327 | /* Nonzero means allow type mismatches in conditional expressions; |
328 | just make their values `void'. */ | |
329 | ||
330 | int flag_cond_mismatch; | |
331 | ||
332 | /* Nonzero means enable C89 Amendment 1 features. */ | |
333 | ||
334 | int flag_isoc94; | |
335 | ||
336 | /* Nonzero means use the ISO C99 dialect of C. */ | |
337 | ||
338 | int flag_isoc99; | |
339 | ||
8b332087 | 340 | /* Nonzero means that we have builtin functions, and main is an int. */ |
574a6990 | 341 | |
342 | int flag_hosted = 1; | |
343 | ||
574a6990 | 344 | /* Warn if main is suspicious. */ |
345 | ||
346 | int warn_main; | |
347 | ||
574a6990 | 348 | |
349 | /* ObjC language option variables. */ | |
350 | ||
351 | ||
352 | /* Open and close the file for outputting class declarations, if | |
353 | requested (ObjC). */ | |
354 | ||
355 | int flag_gen_declaration; | |
356 | ||
574a6990 | 357 | /* Tells the compiler that this is a special run. Do not perform any |
358 | compiling, instead we are to test some platform dependent features | |
359 | and output a C header file with appropriate definitions. */ | |
360 | ||
361 | int print_struct_values; | |
362 | ||
f0b5f617 | 363 | /* Tells the compiler what is the constant string class for ObjC. */ |
574a6990 | 364 | |
365 | const char *constant_string_class_name; | |
366 | ||
574a6990 | 367 | |
368 | /* C++ language option variables. */ | |
369 | ||
370 | ||
371 | /* Nonzero means don't recognize any extension keywords. */ | |
372 | ||
373 | int flag_no_gnu_keywords; | |
374 | ||
375 | /* Nonzero means do emit exported implementations of functions even if | |
376 | they can be inlined. */ | |
377 | ||
378 | int flag_implement_inlines = 1; | |
379 | ||
574a6990 | 380 | /* Nonzero means that implicit instantiations will be emitted if needed. */ |
381 | ||
382 | int flag_implicit_templates = 1; | |
383 | ||
384 | /* Nonzero means that implicit instantiations of inline templates will be | |
385 | emitted if needed, even if instantiations of non-inline templates | |
386 | aren't. */ | |
387 | ||
388 | int flag_implicit_inline_templates = 1; | |
389 | ||
390 | /* Nonzero means generate separate instantiation control files and | |
391 | juggle them at link time. */ | |
392 | ||
393 | int flag_use_repository; | |
394 | ||
395 | /* Nonzero if we want to issue diagnostics that the standard says are not | |
396 | required. */ | |
397 | ||
398 | int flag_optional_diags = 1; | |
399 | ||
400 | /* Nonzero means we should attempt to elide constructors when possible. */ | |
401 | ||
402 | int flag_elide_constructors = 1; | |
403 | ||
404 | /* Nonzero means that member functions defined in class scope are | |
405 | inline by default. */ | |
406 | ||
407 | int flag_default_inline = 1; | |
408 | ||
409 | /* Controls whether compiler generates 'type descriptor' that give | |
410 | run-time type information. */ | |
411 | ||
412 | int flag_rtti = 1; | |
413 | ||
414 | /* Nonzero if we want to conserve space in the .o files. We do this | |
415 | by putting uninitialized data and runtime initialized data into | |
416 | .common instead of .data at the expense of not flagging multiple | |
417 | definitions. */ | |
418 | ||
419 | int flag_conserve_space; | |
420 | ||
421 | /* Nonzero if we want to obey access control semantics. */ | |
422 | ||
423 | int flag_access_control = 1; | |
424 | ||
425 | /* Nonzero if we want to check the return value of new and avoid calling | |
426 | constructors if it is a null pointer. */ | |
427 | ||
428 | int flag_check_new; | |
429 | ||
6dcdb5de | 430 | /* The C++ dialect being used. C++98 is the default. */ |
0fe6eeac | 431 | |
6dcdb5de | 432 | enum cxx_dialect cxx_dialect = cxx98; |
0fe6eeac | 433 | |
574a6990 | 434 | /* Nonzero if we want the new ISO rules for pushing a new scope for `for' |
435 | initialization variables. | |
436 | 0: Old rules, set by -fno-for-scope. | |
437 | 2: New ISO rules, set by -ffor-scope. | |
438 | 1: Try to implement new ISO rules, but with backup compatibility | |
439 | (and warnings). This is the default, for now. */ | |
440 | ||
441 | int flag_new_for_scope = 1; | |
442 | ||
443 | /* Nonzero if we want to emit defined symbols with common-like linkage as | |
444 | weak symbols where possible, in order to conform to C++ semantics. | |
445 | Otherwise, emit them as local symbols. */ | |
446 | ||
447 | int flag_weak = 1; | |
448 | ||
e7aa92b2 | 449 | /* 0 means we want the preprocessor to not emit line directives for |
450 | the current working directory. 1 means we want it to do it. -1 | |
451 | means we should decide depending on whether debugging information | |
452 | is being emitted or not. */ | |
453 | ||
454 | int flag_working_directory = -1; | |
455 | ||
574a6990 | 456 | /* Nonzero to use __cxa_atexit, rather than atexit, to register |
3bf418bd | 457 | destructors for local statics and global objects. '2' means it has been |
458 | set nonzero as a default, not by a command-line flag. */ | |
574a6990 | 459 | |
460 | int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT; | |
461 | ||
3bf418bd | 462 | /* Nonzero to use __cxa_get_exception_ptr in C++ exception-handling |
463 | code. '2' means it has not been set explicitly on the command line. */ | |
464 | ||
465 | int flag_use_cxa_get_exception_ptr = 2; | |
466 | ||
574a6990 | 467 | /* Nonzero means to implement standard semantics for exception |
468 | specifications, calling unexpected if an exception is thrown that | |
469 | doesn't match the specification. Zero means to treat them as | |
470 | assertions and optimize accordingly, but not check them. */ | |
471 | ||
472 | int flag_enforce_eh_specs = 1; | |
473 | ||
4813f5af | 474 | /* Nonzero means to generate thread-safe code for initializing local |
475 | statics. */ | |
476 | ||
477 | int flag_threadsafe_statics = 1; | |
478 | ||
574a6990 | 479 | /* Nonzero means warn about implicit declarations. */ |
480 | ||
481 | int warn_implicit = 1; | |
482 | ||
574a6990 | 483 | /* Maximum template instantiation depth. This limit is rather |
484 | arbitrary, but it exists to limit the time it takes to notice | |
485 | infinite template instantiations. */ | |
486 | ||
487 | int max_tinst_depth = 500; | |
488 | ||
489 | ||
490 | ||
988fc1d1 | 491 | /* The elements of `ridpointers' are identifier nodes for the reserved |
492 | type names and storage classes. It is indexed by a RID_... value. */ | |
493 | tree *ridpointers; | |
494 | ||
1cae46be | 495 | tree (*make_fname_decl) (tree, int); |
9e5a737d | 496 | |
e78703c1 | 497 | /* Nonzero means the expression being parsed will never be evaluated. |
498 | This is a count, since unevaluated expressions can nest. */ | |
499 | int skip_evaluation; | |
500 | ||
2c0e001b | 501 | /* Information about how a function name is generated. */ |
65b7f83f | 502 | struct fname_var_t |
503 | { | |
e99c3a1d | 504 | tree *const decl; /* pointer to the VAR_DECL. */ |
505 | const unsigned rid; /* RID number for the identifier. */ | |
506 | const int pretty; /* How pretty is it? */ | |
65b7f83f | 507 | }; |
508 | ||
2c0e001b | 509 | /* The three ways of getting then name of the current function. */ |
65b7f83f | 510 | |
511 | const struct fname_var_t fname_vars[] = | |
512 | { | |
2c0e001b | 513 | /* C99 compliant __func__, must be first. */ |
65b7f83f | 514 | {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0}, |
2c0e001b | 515 | /* GCC __FUNCTION__ compliant. */ |
65b7f83f | 516 | {&function_name_decl_node, RID_FUNCTION_NAME, 0}, |
2c0e001b | 517 | /* GCC __PRETTY_FUNCTION__ compliant. */ |
65b7f83f | 518 | {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1}, |
519 | {NULL, 0, 0}, | |
520 | }; | |
521 | ||
2ca392fd | 522 | static tree check_case_value (tree); |
523 | static bool check_case_bounds (tree, tree, tree *, tree *); | |
be43ff5a | 524 | |
1cae46be | 525 | static tree handle_packed_attribute (tree *, tree, tree, int, bool *); |
526 | static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *); | |
527 | static tree handle_common_attribute (tree *, tree, tree, int, bool *); | |
528 | static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *); | |
5de92639 | 529 | static tree handle_hot_attribute (tree *, tree, tree, int, bool *); |
530 | static tree handle_cold_attribute (tree *, tree, tree, int, bool *); | |
1cae46be | 531 | static tree handle_noinline_attribute (tree *, tree, tree, int, bool *); |
532 | static tree handle_always_inline_attribute (tree *, tree, tree, int, | |
533 | bool *); | |
1b16fc45 | 534 | static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *); |
535 | static tree handle_artificial_attribute (tree *, tree, tree, int, bool *); | |
0cdd9887 | 536 | static tree handle_flatten_attribute (tree *, tree, tree, int, bool *); |
10fc867f | 537 | static tree handle_error_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 538 | static tree handle_used_attribute (tree *, tree, tree, int, bool *); |
539 | static tree handle_unused_attribute (tree *, tree, tree, int, bool *); | |
62eec3b4 | 540 | static tree handle_externally_visible_attribute (tree *, tree, tree, int, |
541 | bool *); | |
1cae46be | 542 | static tree handle_const_attribute (tree *, tree, tree, int, bool *); |
543 | static tree handle_transparent_union_attribute (tree *, tree, tree, | |
544 | int, bool *); | |
545 | static tree handle_constructor_attribute (tree *, tree, tree, int, bool *); | |
546 | static tree handle_destructor_attribute (tree *, tree, tree, int, bool *); | |
547 | static tree handle_mode_attribute (tree *, tree, tree, int, bool *); | |
548 | static tree handle_section_attribute (tree *, tree, tree, int, bool *); | |
549 | static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); | |
550 | static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; | |
551 | static tree handle_alias_attribute (tree *, tree, tree, int, bool *); | |
f4a30bd7 | 552 | static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ; |
1cae46be | 553 | static tree handle_visibility_attribute (tree *, tree, tree, int, |
554 | bool *); | |
555 | static tree handle_tls_model_attribute (tree *, tree, tree, int, | |
556 | bool *); | |
557 | static tree handle_no_instrument_function_attribute (tree *, tree, | |
558 | tree, int, bool *); | |
559 | static tree handle_malloc_attribute (tree *, tree, tree, int, bool *); | |
26d1c5ff | 560 | static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 561 | static tree handle_no_limit_stack_attribute (tree *, tree, tree, int, |
562 | bool *); | |
563 | static tree handle_pure_attribute (tree *, tree, tree, int, bool *); | |
fc09b200 | 564 | static tree handle_novops_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 565 | static tree handle_deprecated_attribute (tree *, tree, tree, int, |
566 | bool *); | |
567 | static tree handle_vector_size_attribute (tree *, tree, tree, int, | |
568 | bool *); | |
569 | static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *); | |
570 | static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *); | |
571 | static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *); | |
8a8cdb8d | 572 | static tree handle_warn_unused_result_attribute (tree *, tree, tree, int, |
573 | bool *); | |
bf6c8de0 | 574 | static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *); |
b5c26b42 | 575 | static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *); |
4a29c97c | 576 | static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *); |
46f8e3b0 | 577 | static tree handle_option_attribute (tree *, tree, tree, int, bool *); |
578 | static tree handle_optimize_attribute (tree *, tree, tree, int, bool *); | |
1cae46be | 579 | |
d01f58f9 | 580 | static void check_function_nonnull (tree, int, tree *); |
1cae46be | 581 | static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); |
582 | static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT); | |
583 | static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); | |
860251be | 584 | static int resort_field_decl_cmp (const void *, const void *); |
dbf6c367 | 585 | |
5c6e5756 | 586 | /* Reserved words. The third field is a mask: keywords are disabled |
587 | if they match the mask. | |
588 | ||
589 | Masks for languages: | |
590 | C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC | |
591 | C --std=c99: D_CXXONLY | D_OBJC | |
592 | ObjC is like C except that D_OBJC and D_CXX_OBJC are not set | |
593 | C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | |
594 | C++ --std=c0x: D_CONLY | D_OBJC | |
595 | ObjC++ is like C++ except that D_OBJC is not set | |
596 | ||
597 | If -fno-asm is used, D_ASM is added to the mask. If | |
598 | -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in | |
599 | C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords. | |
600 | In C with -Wcxx-compat, we warn if D_CXXWARN is set. */ | |
601 | ||
602 | const struct c_common_resword c_common_reswords[] = | |
603 | { | |
604 | { "_Bool", RID_BOOL, D_CONLY }, | |
605 | { "_Complex", RID_COMPLEX, 0 }, | |
606 | { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT }, | |
607 | { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT }, | |
608 | { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT }, | |
609 | { "_Fract", RID_FRACT, D_CONLY | D_EXT }, | |
610 | { "_Accum", RID_ACCUM, D_CONLY | D_EXT }, | |
611 | { "_Sat", RID_SAT, D_CONLY | D_EXT }, | |
612 | { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, | |
613 | { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, | |
614 | { "__alignof", RID_ALIGNOF, 0 }, | |
615 | { "__alignof__", RID_ALIGNOF, 0 }, | |
616 | { "__asm", RID_ASM, 0 }, | |
617 | { "__asm__", RID_ASM, 0 }, | |
618 | { "__attribute", RID_ATTRIBUTE, 0 }, | |
619 | { "__attribute__", RID_ATTRIBUTE, 0 }, | |
620 | { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, | |
621 | { "__builtin_offsetof", RID_OFFSETOF, 0 }, | |
622 | { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, | |
623 | { "__builtin_va_arg", RID_VA_ARG, 0 }, | |
624 | { "__complex", RID_COMPLEX, 0 }, | |
625 | { "__complex__", RID_COMPLEX, 0 }, | |
626 | { "__const", RID_CONST, 0 }, | |
627 | { "__const__", RID_CONST, 0 }, | |
628 | { "__decltype", RID_DECLTYPE, D_CXXONLY }, | |
629 | { "__extension__", RID_EXTENSION, 0 }, | |
630 | { "__func__", RID_C99_FUNCTION_NAME, 0 }, | |
631 | { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY }, | |
632 | { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY }, | |
633 | { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY }, | |
634 | { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY }, | |
635 | { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY }, | |
636 | { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY }, | |
637 | { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY }, | |
638 | { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY }, | |
639 | { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY }, | |
640 | { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY }, | |
641 | { "__is_class", RID_IS_CLASS, D_CXXONLY }, | |
642 | { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY }, | |
643 | { "__is_empty", RID_IS_EMPTY, D_CXXONLY }, | |
644 | { "__is_enum", RID_IS_ENUM, D_CXXONLY }, | |
645 | { "__is_pod", RID_IS_POD, D_CXXONLY }, | |
646 | { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY }, | |
647 | { "__is_union", RID_IS_UNION, D_CXXONLY }, | |
648 | { "__imag", RID_IMAGPART, 0 }, | |
649 | { "__imag__", RID_IMAGPART, 0 }, | |
650 | { "__inline", RID_INLINE, 0 }, | |
651 | { "__inline__", RID_INLINE, 0 }, | |
652 | { "__label__", RID_LABEL, 0 }, | |
653 | { "__null", RID_NULL, 0 }, | |
654 | { "__real", RID_REALPART, 0 }, | |
655 | { "__real__", RID_REALPART, 0 }, | |
656 | { "__restrict", RID_RESTRICT, 0 }, | |
657 | { "__restrict__", RID_RESTRICT, 0 }, | |
658 | { "__signed", RID_SIGNED, 0 }, | |
659 | { "__signed__", RID_SIGNED, 0 }, | |
660 | { "__thread", RID_THREAD, 0 }, | |
661 | { "__typeof", RID_TYPEOF, 0 }, | |
662 | { "__typeof__", RID_TYPEOF, 0 }, | |
663 | { "__volatile", RID_VOLATILE, 0 }, | |
664 | { "__volatile__", RID_VOLATILE, 0 }, | |
665 | { "asm", RID_ASM, D_ASM }, | |
666 | { "auto", RID_AUTO, 0 }, | |
667 | { "bool", RID_BOOL, D_CXXONLY }, | |
668 | { "break", RID_BREAK, 0 }, | |
669 | { "case", RID_CASE, 0 }, | |
670 | { "catch", RID_CATCH, D_CXX_OBJC }, | |
671 | { "char", RID_CHAR, 0 }, | |
672 | { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X }, | |
673 | { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X }, | |
674 | { "class", RID_CLASS, D_CXX_OBJC }, | |
675 | { "const", RID_CONST, 0 }, | |
676 | { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN }, | |
677 | { "continue", RID_CONTINUE, 0 }, | |
678 | { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X }, | |
679 | { "default", RID_DEFAULT, 0 }, | |
680 | { "delete", RID_DELETE, D_CXXONLY }, | |
681 | { "do", RID_DO, 0 }, | |
682 | { "double", RID_DOUBLE, 0 }, | |
683 | { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN }, | |
684 | { "else", RID_ELSE, 0 }, | |
685 | { "enum", RID_ENUM, 0 }, | |
686 | { "explicit", RID_EXPLICIT, D_CXXONLY }, | |
687 | { "export", RID_EXPORT, D_CXXONLY }, | |
688 | { "extern", RID_EXTERN, 0 }, | |
689 | { "false", RID_FALSE, D_CXXONLY }, | |
690 | { "float", RID_FLOAT, 0 }, | |
691 | { "for", RID_FOR, 0 }, | |
692 | { "friend", RID_FRIEND, D_CXXONLY }, | |
693 | { "goto", RID_GOTO, 0 }, | |
694 | { "if", RID_IF, 0 }, | |
695 | { "inline", RID_INLINE, D_EXT89 }, | |
696 | { "int", RID_INT, 0 }, | |
697 | { "long", RID_LONG, 0 }, | |
698 | { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN }, | |
699 | { "namespace", RID_NAMESPACE, D_CXXONLY }, | |
700 | { "new", RID_NEW, D_CXXONLY }, | |
701 | { "operator", RID_OPERATOR, D_CXXONLY }, | |
702 | { "private", RID_PRIVATE, D_CXX_OBJC }, | |
703 | { "protected", RID_PROTECTED, D_CXX_OBJC }, | |
704 | { "public", RID_PUBLIC, D_CXX_OBJC }, | |
705 | { "register", RID_REGISTER, 0 }, | |
706 | { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN }, | |
707 | { "restrict", RID_RESTRICT, D_CONLY | D_C99 }, | |
708 | { "return", RID_RETURN, 0 }, | |
709 | { "short", RID_SHORT, 0 }, | |
710 | { "signed", RID_SIGNED, 0 }, | |
711 | { "sizeof", RID_SIZEOF, 0 }, | |
712 | { "static", RID_STATIC, 0 }, | |
713 | { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
714 | { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN }, | |
715 | { "struct", RID_STRUCT, 0 }, | |
716 | { "switch", RID_SWITCH, 0 }, | |
717 | { "template", RID_TEMPLATE, D_CXXONLY }, | |
718 | { "this", RID_THIS, D_CXXONLY }, | |
719 | { "throw", RID_THROW, D_CXX_OBJC }, | |
720 | { "true", RID_TRUE, D_CXXONLY }, | |
721 | { "try", RID_TRY, D_CXX_OBJC }, | |
722 | { "typedef", RID_TYPEDEF, 0 }, | |
723 | { "typename", RID_TYPENAME, D_CXXONLY }, | |
724 | { "typeid", RID_TYPEID, D_CXXONLY }, | |
725 | { "typeof", RID_TYPEOF, D_ASM | D_EXT }, | |
726 | { "union", RID_UNION, 0 }, | |
727 | { "unsigned", RID_UNSIGNED, 0 }, | |
728 | { "using", RID_USING, D_CXXONLY }, | |
729 | { "virtual", RID_VIRTUAL, D_CXXONLY }, | |
730 | { "void", RID_VOID, 0 }, | |
731 | { "volatile", RID_VOLATILE, 0 }, | |
732 | { "wchar_t", RID_WCHAR, D_CXXONLY }, | |
733 | { "while", RID_WHILE, 0 }, | |
734 | /* These Objective-C keywords are recognized only immediately after | |
735 | an '@'. */ | |
736 | { "compatibility_alias", RID_AT_ALIAS, D_OBJC }, | |
737 | { "defs", RID_AT_DEFS, D_OBJC }, | |
738 | { "encode", RID_AT_ENCODE, D_OBJC }, | |
739 | { "end", RID_AT_END, D_OBJC }, | |
740 | { "implementation", RID_AT_IMPLEMENTATION, D_OBJC }, | |
741 | { "interface", RID_AT_INTERFACE, D_OBJC }, | |
742 | { "protocol", RID_AT_PROTOCOL, D_OBJC }, | |
743 | { "selector", RID_AT_SELECTOR, D_OBJC }, | |
744 | { "finally", RID_AT_FINALLY, D_OBJC }, | |
745 | { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC }, | |
746 | /* These are recognized only in protocol-qualifier context | |
747 | (see above) */ | |
748 | { "bycopy", RID_BYCOPY, D_OBJC }, | |
749 | { "byref", RID_BYREF, D_OBJC }, | |
750 | { "in", RID_IN, D_OBJC }, | |
751 | { "inout", RID_INOUT, D_OBJC }, | |
752 | { "oneway", RID_ONEWAY, D_OBJC }, | |
753 | { "out", RID_OUT, D_OBJC }, | |
754 | }; | |
755 | ||
756 | const unsigned int num_c_common_reswords = | |
757 | sizeof c_common_reswords / sizeof (struct c_common_resword); | |
758 | ||
f8e93a2e | 759 | /* Table of machine-independent attributes common to all C-like languages. */ |
760 | const struct attribute_spec c_common_attribute_table[] = | |
761 | { | |
762 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */ | |
763 | { "packed", 0, 0, false, false, false, | |
1cae46be | 764 | handle_packed_attribute }, |
f8e93a2e | 765 | { "nocommon", 0, 0, true, false, false, |
766 | handle_nocommon_attribute }, | |
767 | { "common", 0, 0, true, false, false, | |
768 | handle_common_attribute }, | |
769 | /* FIXME: logically, noreturn attributes should be listed as | |
770 | "false, true, true" and apply to function types. But implementing this | |
771 | would require all the places in the compiler that use TREE_THIS_VOLATILE | |
772 | on a decl to identify non-returning functions to be located and fixed | |
773 | to check the function type instead. */ | |
774 | { "noreturn", 0, 0, true, false, false, | |
775 | handle_noreturn_attribute }, | |
776 | { "volatile", 0, 0, true, false, false, | |
777 | handle_noreturn_attribute }, | |
778 | { "noinline", 0, 0, true, false, false, | |
779 | handle_noinline_attribute }, | |
780 | { "always_inline", 0, 0, true, false, false, | |
781 | handle_always_inline_attribute }, | |
541e4101 | 782 | { "gnu_inline", 0, 0, true, false, false, |
783 | handle_gnu_inline_attribute }, | |
1b16fc45 | 784 | { "artificial", 0, 0, true, false, false, |
785 | handle_artificial_attribute }, | |
0cdd9887 | 786 | { "flatten", 0, 0, true, false, false, |
a0c938f0 | 787 | handle_flatten_attribute }, |
f8e93a2e | 788 | { "used", 0, 0, true, false, false, |
789 | handle_used_attribute }, | |
790 | { "unused", 0, 0, false, false, false, | |
791 | handle_unused_attribute }, | |
62eec3b4 | 792 | { "externally_visible", 0, 0, true, false, false, |
793 | handle_externally_visible_attribute }, | |
f8e93a2e | 794 | /* The same comments as for noreturn attributes apply to const ones. */ |
795 | { "const", 0, 0, true, false, false, | |
796 | handle_const_attribute }, | |
797 | { "transparent_union", 0, 0, false, false, false, | |
798 | handle_transparent_union_attribute }, | |
9af7fd5b | 799 | { "constructor", 0, 1, true, false, false, |
f8e93a2e | 800 | handle_constructor_attribute }, |
9af7fd5b | 801 | { "destructor", 0, 1, true, false, false, |
f8e93a2e | 802 | handle_destructor_attribute }, |
803 | { "mode", 1, 1, false, true, false, | |
804 | handle_mode_attribute }, | |
805 | { "section", 1, 1, true, false, false, | |
806 | handle_section_attribute }, | |
807 | { "aligned", 0, 1, false, false, false, | |
808 | handle_aligned_attribute }, | |
809 | { "weak", 0, 0, true, false, false, | |
810 | handle_weak_attribute }, | |
811 | { "alias", 1, 1, true, false, false, | |
812 | handle_alias_attribute }, | |
f4a30bd7 | 813 | { "weakref", 0, 1, true, false, false, |
814 | handle_weakref_attribute }, | |
f8e93a2e | 815 | { "no_instrument_function", 0, 0, true, false, false, |
816 | handle_no_instrument_function_attribute }, | |
817 | { "malloc", 0, 0, true, false, false, | |
818 | handle_malloc_attribute }, | |
26d1c5ff | 819 | { "returns_twice", 0, 0, true, false, false, |
820 | handle_returns_twice_attribute }, | |
f8e93a2e | 821 | { "no_stack_limit", 0, 0, true, false, false, |
822 | handle_no_limit_stack_attribute }, | |
823 | { "pure", 0, 0, true, false, false, | |
824 | handle_pure_attribute }, | |
fc09b200 | 825 | /* For internal use (marking of builtins) only. The name contains space |
826 | to prevent its usage in source code. */ | |
827 | { "no vops", 0, 0, true, false, false, | |
828 | handle_novops_attribute }, | |
f8e93a2e | 829 | { "deprecated", 0, 0, false, false, false, |
830 | handle_deprecated_attribute }, | |
831 | { "vector_size", 1, 1, false, true, false, | |
832 | handle_vector_size_attribute }, | |
b212f378 | 833 | { "visibility", 1, 1, false, false, false, |
f8e93a2e | 834 | handle_visibility_attribute }, |
24dfead4 | 835 | { "tls_model", 1, 1, true, false, false, |
836 | handle_tls_model_attribute }, | |
dbf6c367 | 837 | { "nonnull", 0, -1, false, true, true, |
838 | handle_nonnull_attribute }, | |
fa987697 | 839 | { "nothrow", 0, 0, true, false, false, |
840 | handle_nothrow_attribute }, | |
cb59f969 | 841 | { "may_alias", 0, 0, false, true, false, NULL }, |
7acb29a3 | 842 | { "cleanup", 1, 1, true, false, false, |
843 | handle_cleanup_attribute }, | |
8a8cdb8d | 844 | { "warn_unused_result", 0, 0, false, true, true, |
845 | handle_warn_unused_result_attribute }, | |
50ca527f | 846 | { "sentinel", 0, 1, false, true, true, |
bf6c8de0 | 847 | handle_sentinel_attribute }, |
b5c26b42 | 848 | /* For internal use (marking of builtins) only. The name contains space |
849 | to prevent its usage in source code. */ | |
850 | { "type generic", 0, 0, false, true, true, | |
851 | handle_type_generic_attribute }, | |
4a29c97c | 852 | { "alloc_size", 1, 2, false, true, true, |
853 | handle_alloc_size_attribute }, | |
5de92639 | 854 | { "cold", 0, 0, true, false, false, |
855 | handle_cold_attribute }, | |
856 | { "hot", 0, 0, true, false, false, | |
857 | handle_hot_attribute }, | |
10fc867f | 858 | { "warning", 1, 1, true, false, false, |
859 | handle_error_attribute }, | |
860 | { "error", 1, 1, true, false, false, | |
861 | handle_error_attribute }, | |
46f8e3b0 | 862 | { "option", 1, -1, true, false, false, |
863 | handle_option_attribute }, | |
864 | { "optimize", 1, -1, true, false, false, | |
865 | handle_optimize_attribute }, | |
f8e93a2e | 866 | { NULL, 0, 0, false, false, false, NULL } |
867 | }; | |
868 | ||
869 | /* Give the specifications for the format attributes, used by C and all | |
d716ce75 | 870 | descendants. */ |
f8e93a2e | 871 | |
872 | const struct attribute_spec c_common_format_attribute_table[] = | |
873 | { | |
874 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */ | |
875 | { "format", 3, 3, false, true, true, | |
876 | handle_format_attribute }, | |
877 | { "format_arg", 1, 1, false, true, true, | |
878 | handle_format_arg_attribute }, | |
879 | { NULL, 0, 0, false, false, false, NULL } | |
880 | }; | |
881 | ||
2c0e001b | 882 | /* Push current bindings for the function name VAR_DECLS. */ |
f4e3c278 | 883 | |
884 | void | |
1cae46be | 885 | start_fname_decls (void) |
f4e3c278 | 886 | { |
65b7f83f | 887 | unsigned ix; |
888 | tree saved = NULL_TREE; | |
1cae46be | 889 | |
65b7f83f | 890 | for (ix = 0; fname_vars[ix].decl; ix++) |
891 | { | |
892 | tree decl = *fname_vars[ix].decl; | |
f4e3c278 | 893 | |
65b7f83f | 894 | if (decl) |
895 | { | |
7016c612 | 896 | saved = tree_cons (decl, build_int_cst (NULL_TREE, ix), saved); |
65b7f83f | 897 | *fname_vars[ix].decl = NULL_TREE; |
898 | } | |
899 | } | |
900 | if (saved || saved_function_name_decls) | |
901 | /* Normally they'll have been NULL, so only push if we've got a | |
902 | stack, or they are non-NULL. */ | |
903 | saved_function_name_decls = tree_cons (saved, NULL_TREE, | |
904 | saved_function_name_decls); | |
905 | } | |
906 | ||
2363ef00 | 907 | /* Finish up the current bindings, adding them into the current function's |
908 | statement tree. This must be done _before_ finish_stmt_tree is called. | |
909 | If there is no current function, we must be at file scope and no statements | |
910 | are involved. Pop the previous bindings. */ | |
65b7f83f | 911 | |
912 | void | |
1cae46be | 913 | finish_fname_decls (void) |
65b7f83f | 914 | { |
915 | unsigned ix; | |
2363ef00 | 916 | tree stmts = NULL_TREE; |
65b7f83f | 917 | tree stack = saved_function_name_decls; |
918 | ||
919 | for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack)) | |
2363ef00 | 920 | append_to_statement_list (TREE_VALUE (stack), &stmts); |
1cae46be | 921 | |
2363ef00 | 922 | if (stmts) |
65b7f83f | 923 | { |
2363ef00 | 924 | tree *bodyp = &DECL_SAVED_TREE (current_function_decl); |
5c423bd6 | 925 | |
2363ef00 | 926 | if (TREE_CODE (*bodyp) == BIND_EXPR) |
927 | bodyp = &BIND_EXPR_BODY (*bodyp); | |
81010c97 | 928 | |
bc2b76e0 | 929 | append_to_statement_list_force (*bodyp, &stmts); |
2363ef00 | 930 | *bodyp = stmts; |
65b7f83f | 931 | } |
1cae46be | 932 | |
65b7f83f | 933 | for (ix = 0; fname_vars[ix].decl; ix++) |
934 | *fname_vars[ix].decl = NULL_TREE; | |
1cae46be | 935 | |
65b7f83f | 936 | if (stack) |
f4e3c278 | 937 | { |
2c0e001b | 938 | /* We had saved values, restore them. */ |
65b7f83f | 939 | tree saved; |
940 | ||
941 | for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved)) | |
942 | { | |
943 | tree decl = TREE_PURPOSE (saved); | |
944 | unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved)); | |
1cae46be | 945 | |
65b7f83f | 946 | *fname_vars[ix].decl = decl; |
947 | } | |
948 | stack = TREE_CHAIN (stack); | |
f4e3c278 | 949 | } |
65b7f83f | 950 | saved_function_name_decls = stack; |
951 | } | |
952 | ||
81010c97 | 953 | /* Return the text name of the current function, suitably prettified |
5fc7fa69 | 954 | by PRETTY_P. Return string must be freed by caller. */ |
65b7f83f | 955 | |
956 | const char * | |
1cae46be | 957 | fname_as_string (int pretty_p) |
65b7f83f | 958 | { |
9ad4bb1e | 959 | const char *name = "top level"; |
5fc7fa69 | 960 | char *namep; |
8115b8be | 961 | int vrb = 2, len; |
962 | cpp_string cstr = { 0, 0 }, strname; | |
9ad4bb1e | 963 | |
84166705 | 964 | if (!pretty_p) |
9ad4bb1e | 965 | { |
966 | name = ""; | |
967 | vrb = 0; | |
968 | } | |
969 | ||
970 | if (current_function_decl) | |
dc24ddbd | 971 | name = lang_hooks.decl_printable_name (current_function_decl, vrb); |
9ad4bb1e | 972 | |
8115b8be | 973 | len = strlen (name) + 3; /* Two for '"'s. One for NULL. */ |
5fc7fa69 | 974 | |
8115b8be | 975 | namep = XNEWVEC (char, len); |
976 | snprintf (namep, len, "\"%s\"", name); | |
977 | strname.text = (unsigned char *) namep; | |
978 | strname.len = len - 1; | |
5fc7fa69 | 979 | |
924bbf02 | 980 | if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING)) |
8115b8be | 981 | { |
982 | XDELETEVEC (namep); | |
983 | return (const char *) cstr.text; | |
5fc7fa69 | 984 | } |
5fc7fa69 | 985 | |
986 | return namep; | |
65b7f83f | 987 | } |
988 | ||
05f9ea78 | 989 | /* Expand DECL if it declares an entity not handled by the |
990 | common code. */ | |
991 | ||
992 | int | |
993 | c_expand_decl (tree decl) | |
994 | { | |
995 | if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)) | |
996 | { | |
997 | /* Let the back-end know about this variable. */ | |
998 | if (!anon_aggr_type_p (TREE_TYPE (decl))) | |
a0c938f0 | 999 | emit_local_var (decl); |
05f9ea78 | 1000 | else |
a0c938f0 | 1001 | expand_anon_union_decl (decl, NULL_TREE, |
1002 | DECL_ANON_UNION_ELEMS (decl)); | |
05f9ea78 | 1003 | } |
05f9ea78 | 1004 | else |
1005 | return 0; | |
1006 | ||
1007 | return 1; | |
1008 | } | |
1009 | ||
1010 | ||
65b7f83f | 1011 | /* Return the VAR_DECL for a const char array naming the current |
1012 | function. If the VAR_DECL has not yet been created, create it | |
1013 | now. RID indicates how it should be formatted and IDENTIFIER_NODE | |
1014 | ID is its name (unfortunately C and C++ hold the RID values of | |
1015 | keywords in different places, so we can't derive RID from ID in | |
dd5b4b36 | 1016 | this language independent code. */ |
65b7f83f | 1017 | |
1018 | tree | |
1cae46be | 1019 | fname_decl (unsigned int rid, tree id) |
65b7f83f | 1020 | { |
1021 | unsigned ix; | |
1022 | tree decl = NULL_TREE; | |
1023 | ||
1024 | for (ix = 0; fname_vars[ix].decl; ix++) | |
1025 | if (fname_vars[ix].rid == rid) | |
1026 | break; | |
1027 | ||
1028 | decl = *fname_vars[ix].decl; | |
1029 | if (!decl) | |
f4e3c278 | 1030 | { |
2222b3c6 | 1031 | /* If a tree is built here, it would normally have the lineno of |
1032 | the current statement. Later this tree will be moved to the | |
1033 | beginning of the function and this line number will be wrong. | |
1034 | To avoid this problem set the lineno to 0 here; that prevents | |
7299020b | 1035 | it from appearing in the RTL. */ |
2363ef00 | 1036 | tree stmts; |
9a6486a6 | 1037 | location_t saved_location = input_location; |
9a6486a6 | 1038 | input_location = UNKNOWN_LOCATION; |
1cae46be | 1039 | |
2363ef00 | 1040 | stmts = push_stmt_list (); |
65b7f83f | 1041 | decl = (*make_fname_decl) (id, fname_vars[ix].pretty); |
2363ef00 | 1042 | stmts = pop_stmt_list (stmts); |
1043 | if (!IS_EMPTY_STMT (stmts)) | |
1044 | saved_function_name_decls | |
1045 | = tree_cons (decl, stmts, saved_function_name_decls); | |
65b7f83f | 1046 | *fname_vars[ix].decl = decl; |
9a6486a6 | 1047 | input_location = saved_location; |
f4e3c278 | 1048 | } |
65b7f83f | 1049 | if (!ix && !current_function_decl) |
8864917d | 1050 | pedwarn (0, "%qD is not defined outside of function scope", decl); |
81010c97 | 1051 | |
65b7f83f | 1052 | return decl; |
f4e3c278 | 1053 | } |
1054 | ||
070236f0 | 1055 | /* Given a STRING_CST, give it a suitable array-of-chars data type. */ |
b0fc3e72 | 1056 | |
1057 | tree | |
1cae46be | 1058 | fix_string_type (tree value) |
b0fc3e72 | 1059 | { |
070236f0 | 1060 | int length = TREE_STRING_LENGTH (value); |
1061 | int nchars; | |
00d26680 | 1062 | tree e_type, i_type, a_type; |
1063 | ||
73be5127 | 1064 | /* Compute the number of elements, for the array type. */ |
924bbf02 | 1065 | if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value)) |
1066 | { | |
1067 | nchars = length; | |
1068 | e_type = char_type_node; | |
1069 | } | |
1070 | else if (TREE_TYPE (value) == char16_array_type_node) | |
1071 | { | |
1072 | nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT); | |
1073 | e_type = char16_type_node; | |
1074 | } | |
1075 | else if (TREE_TYPE (value) == char32_array_type_node) | |
1076 | { | |
1077 | nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT); | |
1078 | e_type = char32_type_node; | |
1079 | } | |
1080 | else | |
1081 | { | |
1082 | nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT); | |
1083 | e_type = wchar_type_node; | |
1084 | } | |
b0fc3e72 | 1085 | |
1d752508 | 1086 | /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous |
1087 | limit in C++98 Annex B is very large (65536) and is not normative, | |
1088 | so we do not diagnose it (warn_overlength_strings is forced off | |
1089 | in c_common_post_options). */ | |
1090 | if (warn_overlength_strings) | |
1091 | { | |
1092 | const int nchars_max = flag_isoc99 ? 4095 : 509; | |
1093 | const int relevant_std = flag_isoc99 ? 99 : 90; | |
1094 | if (nchars - 1 > nchars_max) | |
1095 | /* Translators: The %d after 'ISO C' will be 90 or 99. Do not | |
1096 | separate the %d from the 'C'. 'ISO' should not be | |
1097 | translated, but it may be moved after 'C%d' in languages | |
1098 | where modifiers follow nouns. */ | |
8864917d | 1099 | pedwarn (OPT_Woverlength_strings, |
1100 | "string length %qd is greater than the length %qd " | |
1d752508 | 1101 | "ISO C%d compilers are required to support", |
1102 | nchars - 1, nchars_max, relevant_std); | |
1103 | } | |
82cfc7f7 | 1104 | |
390be14e | 1105 | /* Create the array type for the string constant. The ISO C++ |
1106 | standard says that a string literal has type `const char[N]' or | |
1107 | `const wchar_t[N]'. We use the same logic when invoked as a C | |
1108 | front-end with -Wwrite-strings. | |
1109 | ??? We should change the type of an expression depending on the | |
1110 | state of a warning flag. We should just be warning -- see how | |
1111 | this is handled in the C++ front-end for the deprecated implicit | |
1112 | conversion from string literals to `char*' or `wchar_t*'. | |
00d26680 | 1113 | |
1114 | The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified | |
1115 | array type being the unqualified version of that type. | |
1116 | Therefore, if we are constructing an array of const char, we must | |
1117 | construct the matching unqualified array type first. The C front | |
1118 | end does not require this, but it does no harm, so we do it | |
1119 | unconditionally. */ | |
7016c612 | 1120 | i_type = build_index_type (build_int_cst (NULL_TREE, nchars - 1)); |
00d26680 | 1121 | a_type = build_array_type (e_type, i_type); |
390be14e | 1122 | if (c_dialect_cxx() || warn_write_strings) |
aebc8537 | 1123 | a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST); |
3a10ba35 | 1124 | |
00d26680 | 1125 | TREE_TYPE (value) = a_type; |
b8e3b7ad | 1126 | TREE_CONSTANT (value) = 1; |
a814bad5 | 1127 | TREE_READONLY (value) = 1; |
b0fc3e72 | 1128 | TREE_STATIC (value) = 1; |
1129 | return value; | |
1130 | } | |
1131 | \f | |
2a1736ed | 1132 | /* Print a warning if a constant expression had overflow in folding. |
1133 | Invoke this function on every expression that the language | |
1134 | requires to be a constant expression. | |
1135 | Note the ANSI C standard says it is erroneous for a | |
1136 | constant expression to overflow. */ | |
b2806639 | 1137 | |
1138 | void | |
1cae46be | 1139 | constant_expression_warning (tree value) |
07317e69 | 1140 | { |
1141 | if (warn_overflow && pedantic | |
1142 | && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST | |
1143 | || TREE_CODE (value) == FIXED_CST | |
1144 | || TREE_CODE (value) == VECTOR_CST | |
1145 | || TREE_CODE (value) == COMPLEX_CST) | |
1146 | && TREE_OVERFLOW (value)) | |
8864917d | 1147 | pedwarn (OPT_Woverflow, "overflow in constant expression"); |
07317e69 | 1148 | } |
1149 | ||
1150 | /* The same as above but print an unconditional error. */ | |
1151 | void | |
1152 | constant_expression_error (tree value) | |
b2806639 | 1153 | { |
837e1122 | 1154 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
9421ebb9 | 1155 | || TREE_CODE (value) == FIXED_CST |
886cfd4f | 1156 | || TREE_CODE (value) == VECTOR_CST |
837e1122 | 1157 | || TREE_CODE (value) == COMPLEX_CST) |
07317e69 | 1158 | && TREE_OVERFLOW (value)) |
1159 | error ("overflow in constant expression"); | |
2a1736ed | 1160 | } |
1161 | ||
f170d67f | 1162 | /* Print a warning if an expression had overflow in folding and its |
1163 | operands hadn't. | |
1164 | ||
2a1736ed | 1165 | Invoke this function on every expression that |
1166 | (1) appears in the source code, and | |
f170d67f | 1167 | (2) is a constant expression that overflowed, and |
2a1736ed | 1168 | (3) is not already checked by convert_and_check; |
f170d67f | 1169 | however, do not invoke this function on operands of explicit casts |
1170 | or when the expression is the result of an operator and any operand | |
1171 | already overflowed. */ | |
2a1736ed | 1172 | |
1173 | void | |
1cae46be | 1174 | overflow_warning (tree value) |
2a1736ed | 1175 | { |
f170d67f | 1176 | if (skip_evaluation) return; |
1177 | ||
1178 | switch (TREE_CODE (value)) | |
886cfd4f | 1179 | { |
f170d67f | 1180 | case INTEGER_CST: |
1181 | warning (OPT_Woverflow, "integer overflow in expression"); | |
1182 | break; | |
1183 | ||
1184 | case REAL_CST: | |
1185 | warning (OPT_Woverflow, "floating point overflow in expression"); | |
1186 | break; | |
1187 | ||
9421ebb9 | 1188 | case FIXED_CST: |
1189 | warning (OPT_Woverflow, "fixed-point overflow in expression"); | |
1190 | break; | |
1191 | ||
f170d67f | 1192 | case VECTOR_CST: |
1193 | warning (OPT_Woverflow, "vector overflow in expression"); | |
1194 | break; | |
1195 | ||
1196 | case COMPLEX_CST: | |
1197 | if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) | |
1198 | warning (OPT_Woverflow, "complex integer overflow in expression"); | |
1199 | else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) | |
1200 | warning (OPT_Woverflow, "complex floating point overflow in expression"); | |
1201 | break; | |
1202 | ||
1203 | default: | |
1204 | break; | |
886cfd4f | 1205 | } |
2a1736ed | 1206 | } |
1207 | ||
b13d1547 | 1208 | |
1209 | /* Warn about use of a logical || / && operator being used in a | |
1210 | context where it is likely that the bitwise equivalent was intended | |
1211 | by the programmer. CODE is the TREE_CODE of the operator, ARG1 | |
1212 | and ARG2 the arguments. */ | |
1213 | ||
1214 | void | |
1215 | warn_logical_operator (enum tree_code code, tree arg1, tree | |
1216 | arg2) | |
1217 | { | |
1218 | switch (code) | |
1219 | { | |
1220 | case TRUTH_ANDIF_EXPR: | |
1221 | case TRUTH_ORIF_EXPR: | |
1222 | case TRUTH_OR_EXPR: | |
1223 | case TRUTH_AND_EXPR: | |
e6978426 | 1224 | if (!TREE_NO_WARNING (arg1) |
1225 | && INTEGRAL_TYPE_P (TREE_TYPE (arg1)) | |
1226 | && !CONSTANT_CLASS_P (arg1) | |
1227 | && TREE_CODE (arg2) == INTEGER_CST | |
1228 | && !integer_zerop (arg2)) | |
1229 | { | |
1230 | warning (OPT_Wlogical_op, | |
1231 | "logical %<%s%> with non-zero constant " | |
1232 | "will always evaluate as true", | |
1233 | ((code == TRUTH_ANDIF_EXPR) | |
1234 | || (code == TRUTH_AND_EXPR)) ? "&&" : "||"); | |
1235 | TREE_NO_WARNING (arg1) = true; | |
1236 | } | |
1237 | break; | |
b13d1547 | 1238 | default: |
e6978426 | 1239 | break; |
b13d1547 | 1240 | } |
1241 | } | |
1242 | ||
1243 | ||
bcf22371 | 1244 | /* Print a warning about casts that might indicate violation |
1245 | of strict aliasing rules if -Wstrict-aliasing is used and | |
1e31ff37 | 1246 | strict aliasing mode is in effect. OTYPE is the original |
1247 | TREE_TYPE of EXPR, and TYPE the type we're casting to. */ | |
bcf22371 | 1248 | |
e6fa0ea6 | 1249 | bool |
1e31ff37 | 1250 | strict_aliasing_warning (tree otype, tree type, tree expr) |
bcf22371 | 1251 | { |
f06537f2 | 1252 | if (!(flag_strict_aliasing |
1253 | && POINTER_TYPE_P (type) | |
1254 | && POINTER_TYPE_P (otype) | |
1255 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1256 | /* If the type we are casting to is a ref-all pointer | |
1257 | dereferencing it is always valid. */ | |
1258 | || TYPE_REF_CAN_ALIAS_ALL (type)) | |
e6fa0ea6 | 1259 | return false; |
1260 | ||
1261 | if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR | |
bcf22371 | 1262 | && (DECL_P (TREE_OPERAND (expr, 0)) |
e6fa0ea6 | 1263 | || handled_component_p (TREE_OPERAND (expr, 0)))) |
bcf22371 | 1264 | { |
1265 | /* Casting the address of an object to non void pointer. Warn | |
1266 | if the cast breaks type based aliasing. */ | |
e6fa0ea6 | 1267 | if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) |
1268 | { | |
1269 | warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " | |
1270 | "might break strict-aliasing rules"); | |
1271 | return true; | |
1272 | } | |
bcf22371 | 1273 | else |
1274 | { | |
e6fa0ea6 | 1275 | /* warn_strict_aliasing >= 3. This includes the default (3). |
1276 | Only warn if the cast is dereferenced immediately. */ | |
32c2fdea | 1277 | alias_set_type set1 = |
e6fa0ea6 | 1278 | get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); |
32c2fdea | 1279 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); |
bcf22371 | 1280 | |
62d823d0 | 1281 | if (set1 != set2 && set2 != 0 |
1282 | && (set1 == 0 || !alias_sets_conflict_p (set1, set2))) | |
e6fa0ea6 | 1283 | { |
1284 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1285 | "pointer will break strict-aliasing rules"); | |
1286 | return true; | |
1287 | } | |
1288 | else if (warn_strict_aliasing == 2 | |
879f881c | 1289 | && !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1290 | { |
1291 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1292 | "pointer might break strict-aliasing rules"); | |
1293 | return true; | |
1294 | } | |
bcf22371 | 1295 | } |
1296 | } | |
e6fa0ea6 | 1297 | else |
1298 | if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) | |
1299 | { | |
1300 | /* At this level, warn for any conversions, even if an address is | |
1301 | not taken in the same statement. This will likely produce many | |
1302 | false positives, but could be useful to pinpoint problems that | |
1303 | are not revealed at higher levels. */ | |
32c2fdea | 1304 | alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); |
1305 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); | |
1306 | if (!COMPLETE_TYPE_P (type) | |
879f881c | 1307 | || !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1308 | { |
1309 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1310 | "pointer might break strict-aliasing rules"); | |
1311 | return true; | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | return false; | |
bcf22371 | 1316 | } |
1317 | ||
50247dd9 | 1318 | /* Print a warning about if (); or if () .. else; constructs |
1319 | via the special empty statement node that we create. INNER_THEN | |
1320 | and INNER_ELSE are the statement lists of the if and the else | |
1321 | block. */ | |
1322 | ||
1323 | void | |
ffe8fd56 | 1324 | empty_if_body_warning (tree inner_then, tree inner_else) |
50247dd9 | 1325 | { |
ffe8fd56 | 1326 | if (TREE_CODE (inner_then) == STATEMENT_LIST |
1327 | && STATEMENT_LIST_TAIL (inner_then)) | |
1328 | inner_then = STATEMENT_LIST_TAIL (inner_then)->stmt; | |
1329 | ||
1330 | if (inner_else && TREE_CODE (inner_else) == STATEMENT_LIST | |
1331 | && STATEMENT_LIST_TAIL (inner_else)) | |
1332 | inner_else = STATEMENT_LIST_TAIL (inner_else)->stmt; | |
1333 | ||
1334 | if (IS_EMPTY_STMT (inner_then) && !inner_else) | |
1335 | warning (OPT_Wempty_body, "%Hsuggest braces around empty body " | |
1336 | "in an %<if%> statement", EXPR_LOCUS (inner_then)); | |
1337 | ||
1338 | else if (inner_else && IS_EMPTY_STMT (inner_else)) | |
1339 | warning (OPT_Wempty_body, "%Hsuggest braces around empty body " | |
1340 | "in an %<else%> statement", EXPR_LOCUS (inner_else)); | |
50247dd9 | 1341 | } |
1342 | ||
3f08e399 | 1343 | /* Warn for unlikely, improbable, or stupid DECL declarations |
1344 | of `main'. */ | |
1345 | ||
1346 | void | |
1347 | check_main_parameter_types (tree decl) | |
1348 | { | |
1349 | tree args; | |
1350 | int argct = 0; | |
1351 | ||
1352 | for (args = TYPE_ARG_TYPES (TREE_TYPE (decl)); args; | |
1353 | args = TREE_CHAIN (args)) | |
1354 | { | |
1355 | tree type = args ? TREE_VALUE (args) : 0; | |
1356 | ||
7accad14 | 1357 | if (type == void_type_node || type == error_mark_node ) |
3f08e399 | 1358 | break; |
1359 | ||
1360 | ++argct; | |
1361 | switch (argct) | |
1362 | { | |
1363 | case 1: | |
1364 | if (TYPE_MAIN_VARIANT (type) != integer_type_node) | |
8864917d | 1365 | pedwarn (0, "first argument of %q+D should be %<int%>", decl); |
3f08e399 | 1366 | break; |
1367 | ||
1368 | case 2: | |
1369 | if (TREE_CODE (type) != POINTER_TYPE | |
1370 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
1371 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
1372 | != char_type_node)) | |
8864917d | 1373 | pedwarn (0, "second argument of %q+D should be %<char **%>", |
1374 | decl); | |
3f08e399 | 1375 | break; |
1376 | ||
1377 | case 3: | |
1378 | if (TREE_CODE (type) != POINTER_TYPE | |
1379 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
1380 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
1381 | != char_type_node)) | |
8864917d | 1382 | pedwarn (0, "third argument of %q+D should probably be " |
1383 | "%<char **%>", decl); | |
3f08e399 | 1384 | break; |
1385 | } | |
1386 | } | |
1387 | ||
1388 | /* It is intentional that this message does not mention the third | |
1389 | argument because it's only mentioned in an appendix of the | |
1390 | standard. */ | |
1391 | if (argct > 0 && (argct < 2 || argct > 3)) | |
8864917d | 1392 | pedwarn (0, "%q+D takes only zero or two arguments", decl); |
3f08e399 | 1393 | } |
1394 | ||
ed7c4e62 | 1395 | /* True if pointers to distinct types T1 and T2 can be converted to |
1396 | each other without an explicit cast. Only returns true for opaque | |
1397 | vector types. */ | |
1398 | bool | |
1399 | vector_targets_convertible_p (const_tree t1, const_tree t2) | |
1400 | { | |
1401 | if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE | |
1402 | && (targetm.vector_opaque_p (t1) || targetm.vector_opaque_p (t2)) | |
1403 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) | |
1404 | return true; | |
1405 | ||
1406 | return false; | |
1407 | } | |
1408 | ||
546c4794 | 1409 | /* True if vector types T1 and T2 can be converted to each other |
1410 | without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2 | |
1411 | can only be converted with -flax-vector-conversions yet that is not | |
1412 | in effect, emit a note telling the user about that option if such | |
1413 | a note has not previously been emitted. */ | |
1414 | bool | |
9f627b1a | 1415 | vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note) |
8b4b9810 | 1416 | { |
546c4794 | 1417 | static bool emitted_lax_note = false; |
ae6db8ab | 1418 | bool convertible_lax; |
1419 | ||
1420 | if ((targetm.vector_opaque_p (t1) || targetm.vector_opaque_p (t2)) | |
1421 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) | |
1422 | return true; | |
1423 | ||
1424 | convertible_lax = | |
1425 | (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) | |
1426 | && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || | |
1427 | TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) | |
1428 | && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) | |
1429 | == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); | |
546c4794 | 1430 | |
1431 | if (!convertible_lax || flag_lax_vector_conversions) | |
1432 | return convertible_lax; | |
1433 | ||
1434 | if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) | |
29f51994 | 1435 | && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
546c4794 | 1436 | return true; |
1437 | ||
1438 | if (emit_lax_note && !emitted_lax_note) | |
1439 | { | |
1440 | emitted_lax_note = true; | |
1441 | inform ("use -flax-vector-conversions to permit " | |
1442 | "conversions between vectors with differing " | |
1443 | "element types or numbers of subparts"); | |
1444 | } | |
1445 | ||
1446 | return false; | |
8b4b9810 | 1447 | } |
1448 | ||
d31d55f0 | 1449 | /* Warns if the conversion of EXPR to TYPE may alter a value. |
59dd8856 | 1450 | This is a helper function for warnings_for_convert_and_check. */ |
d31d55f0 | 1451 | |
1452 | static void | |
1453 | conversion_warning (tree type, tree expr) | |
1454 | { | |
1455 | bool give_warning = false; | |
1456 | ||
1457 | unsigned int formal_prec = TYPE_PRECISION (type); | |
1458 | ||
7ee0d227 | 1459 | if (!warn_conversion && !warn_sign_conversion) |
1460 | return; | |
1461 | ||
d31d55f0 | 1462 | if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST) |
1463 | { | |
1464 | /* Warn for real constant that is not an exact integer converted | |
1465 | to integer type. */ | |
1466 | if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE | |
1467 | && TREE_CODE (type) == INTEGER_TYPE) | |
1468 | { | |
1469 | if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (TREE_TYPE (expr)))) | |
1470 | give_warning = true; | |
1471 | } | |
da1fb07b | 1472 | /* Warn for an integer constant that does not fit into integer type. */ |
d31d55f0 | 1473 | else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE |
1474 | && TREE_CODE (type) == INTEGER_TYPE | |
da1fb07b | 1475 | && !int_fits_type_p (expr, type)) |
1476 | { | |
1477 | if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (TREE_TYPE (expr))) | |
7ee0d227 | 1478 | warning (OPT_Wsign_conversion, |
1479 | "negative integer implicitly converted to unsigned type"); | |
1480 | else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (TREE_TYPE (expr))) | |
1481 | warning (OPT_Wsign_conversion, | |
1482 | "conversion of unsigned constant value to negative integer"); | |
1483 | else | |
1484 | give_warning = true; | |
da1fb07b | 1485 | } |
d31d55f0 | 1486 | else if (TREE_CODE (type) == REAL_TYPE) |
1487 | { | |
1488 | /* Warn for an integer constant that does not fit into real type. */ | |
1489 | if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE) | |
1490 | { | |
1491 | REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); | |
1492 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
1493 | give_warning = true; | |
1494 | } | |
1495 | /* Warn for a real constant that does not fit into a smaller | |
1496 | real type. */ | |
1497 | else if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE | |
1498 | && formal_prec < TYPE_PRECISION (TREE_TYPE (expr))) | |
1499 | { | |
1500 | REAL_VALUE_TYPE a = TREE_REAL_CST (expr); | |
1501 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
1502 | give_warning = true; | |
1503 | } | |
1504 | } | |
1505 | ||
1506 | if (give_warning) | |
1507 | warning (OPT_Wconversion, | |
1508 | "conversion to %qT alters %qT constant value", | |
1509 | type, TREE_TYPE (expr)); | |
1510 | } | |
1511 | else /* 'expr' is not a constant. */ | |
1512 | { | |
1513 | /* Warn for real types converted to integer types. */ | |
1514 | if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE | |
1515 | && TREE_CODE (type) == INTEGER_TYPE) | |
1516 | give_warning = true; | |
1517 | ||
1518 | else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
1519 | && TREE_CODE (type) == INTEGER_TYPE) | |
1520 | { | |
69609004 | 1521 | /* Don't warn about unsigned char y = 0xff, x = (int) y; */ |
f9d856a4 | 1522 | expr = get_unwidened (expr, 0); |
69609004 | 1523 | |
d31d55f0 | 1524 | /* Warn for integer types converted to smaller integer types. */ |
7ee0d227 | 1525 | if (formal_prec < TYPE_PRECISION (TREE_TYPE (expr))) |
1526 | give_warning = true; | |
1527 | ||
1528 | /* When they are the same width but different signedness, | |
1529 | then the value may change. */ | |
1530 | else if ((formal_prec == TYPE_PRECISION (TREE_TYPE (expr)) | |
f9d856a4 | 1531 | && TYPE_UNSIGNED (TREE_TYPE (expr)) != TYPE_UNSIGNED (type)) |
7ee0d227 | 1532 | /* Even when converted to a bigger type, if the type is |
1533 | unsigned but expr is signed, then negative values | |
1534 | will be changed. */ | |
f9d856a4 | 1535 | || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (TREE_TYPE (expr)))) |
1536 | warning (OPT_Wsign_conversion, | |
1537 | "conversion to %qT from %qT may change the sign of the result", | |
1538 | type, TREE_TYPE (expr)); | |
d31d55f0 | 1539 | } |
1540 | ||
1541 | /* Warn for integer types converted to real types if and only if | |
1542 | all the range of values of the integer type cannot be | |
1543 | represented by the real type. */ | |
1544 | else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
1545 | && TREE_CODE (type) == REAL_TYPE) | |
1546 | { | |
1547 | tree type_low_bound = TYPE_MIN_VALUE (TREE_TYPE (expr)); | |
1548 | tree type_high_bound = TYPE_MAX_VALUE (TREE_TYPE (expr)); | |
1549 | REAL_VALUE_TYPE real_low_bound = real_value_from_int_cst (0, type_low_bound); | |
1550 | REAL_VALUE_TYPE real_high_bound = real_value_from_int_cst (0, type_high_bound); | |
1551 | ||
1552 | if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound) | |
1553 | || !exact_real_truncate (TYPE_MODE (type), &real_high_bound)) | |
1554 | give_warning = true; | |
1555 | } | |
1556 | ||
1557 | /* Warn for real types converted to smaller real types. */ | |
1558 | else if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE | |
1559 | && TREE_CODE (type) == REAL_TYPE | |
1560 | && formal_prec < TYPE_PRECISION (TREE_TYPE (expr))) | |
1561 | give_warning = true; | |
1562 | ||
1563 | ||
1564 | if (give_warning) | |
1565 | warning (OPT_Wconversion, | |
1566 | "conversion to %qT from %qT may alter its value", | |
1567 | type, TREE_TYPE (expr)); | |
1568 | } | |
1569 | } | |
1570 | ||
59dd8856 | 1571 | /* Produce warnings after a conversion. RESULT is the result of |
1572 | converting EXPR to TYPE. This is a helper function for | |
1573 | convert_and_check and cp_convert_and_check. */ | |
2a1736ed | 1574 | |
59dd8856 | 1575 | void |
1576 | warnings_for_convert_and_check (tree type, tree expr, tree result) | |
2a1736ed | 1577 | { |
da1fb07b | 1578 | if (TREE_CODE (expr) == INTEGER_CST |
1579 | && (TREE_CODE (type) == INTEGER_TYPE | |
1580 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
1581 | && !int_fits_type_p (expr, type)) | |
1582 | { | |
d31d55f0 | 1583 | /* Do not diagnose overflow in a constant expression merely |
1584 | because a conversion overflowed. */ | |
da1fb07b | 1585 | if (TREE_OVERFLOW (result)) |
eddad94a | 1586 | TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); |
1587 | ||
da1fb07b | 1588 | if (TYPE_UNSIGNED (type)) |
d31d55f0 | 1589 | { |
da1fb07b | 1590 | /* This detects cases like converting -129 or 256 to |
1591 | unsigned char. */ | |
1592 | if (!int_fits_type_p (expr, c_common_signed_type (type))) | |
1593 | warning (OPT_Woverflow, | |
1594 | "large integer implicitly truncated to unsigned type"); | |
7ee0d227 | 1595 | else |
da1fb07b | 1596 | conversion_warning (type, expr); |
1597 | } | |
11773141 | 1598 | else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) |
e0913805 | 1599 | warning (OPT_Woverflow, |
1600 | "overflow in implicit constant conversion"); | |
1601 | /* No warning for converting 0x80000000 to int. */ | |
1602 | else if (pedantic | |
1603 | && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE | |
1604 | || TYPE_PRECISION (TREE_TYPE (expr)) | |
1605 | != TYPE_PRECISION (type))) | |
1606 | warning (OPT_Woverflow, | |
1607 | "overflow in implicit constant conversion"); | |
1608 | ||
7ee0d227 | 1609 | else |
e0913805 | 1610 | conversion_warning (type, expr); |
2a1736ed | 1611 | } |
9421ebb9 | 1612 | else if ((TREE_CODE (result) == INTEGER_CST |
1613 | || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) | |
da1fb07b | 1614 | warning (OPT_Woverflow, |
1615 | "overflow in implicit constant conversion"); | |
7ee0d227 | 1616 | else |
da1fb07b | 1617 | conversion_warning (type, expr); |
59dd8856 | 1618 | } |
1619 | ||
1620 | ||
1621 | /* Convert EXPR to TYPE, warning about conversion problems with constants. | |
1622 | Invoke this function on every expression that is converted implicitly, | |
1623 | i.e. because of language rules and not because of an explicit cast. */ | |
1624 | ||
1625 | tree | |
1626 | convert_and_check (tree type, tree expr) | |
1627 | { | |
1628 | tree result; | |
1629 | ||
1630 | if (TREE_TYPE (expr) == type) | |
1631 | return expr; | |
da1fb07b | 1632 | |
59dd8856 | 1633 | result = convert (type, expr); |
1634 | ||
7ee0d227 | 1635 | if (!skip_evaluation && !TREE_OVERFLOW_P (expr) && result != error_mark_node) |
59dd8856 | 1636 | warnings_for_convert_and_check (type, expr, result); |
1637 | ||
da1fb07b | 1638 | return result; |
b2806639 | 1639 | } |
1640 | \f | |
4e91a871 | 1641 | /* A node in a list that describes references to variables (EXPR), which are |
1642 | either read accesses if WRITER is zero, or write accesses, in which case | |
1643 | WRITER is the parent of EXPR. */ | |
1644 | struct tlist | |
1645 | { | |
1646 | struct tlist *next; | |
1647 | tree expr, writer; | |
1648 | }; | |
1649 | ||
1650 | /* Used to implement a cache the results of a call to verify_tree. We only | |
1651 | use this for SAVE_EXPRs. */ | |
1652 | struct tlist_cache | |
1653 | { | |
1654 | struct tlist_cache *next; | |
1655 | struct tlist *cache_before_sp; | |
1656 | struct tlist *cache_after_sp; | |
1657 | tree expr; | |
481c6ce6 | 1658 | }; |
1659 | ||
4e91a871 | 1660 | /* Obstack to use when allocating tlist structures, and corresponding |
1661 | firstobj. */ | |
1662 | static struct obstack tlist_obstack; | |
1663 | static char *tlist_firstobj = 0; | |
1664 | ||
1665 | /* Keep track of the identifiers we've warned about, so we can avoid duplicate | |
1666 | warnings. */ | |
1667 | static struct tlist *warned_ids; | |
1668 | /* SAVE_EXPRs need special treatment. We process them only once and then | |
1669 | cache the results. */ | |
1670 | static struct tlist_cache *save_expr_cache; | |
1671 | ||
1cae46be | 1672 | static void add_tlist (struct tlist **, struct tlist *, tree, int); |
1673 | static void merge_tlist (struct tlist **, struct tlist *, int); | |
1674 | static void verify_tree (tree, struct tlist **, struct tlist **, tree); | |
1675 | static int warning_candidate_p (tree); | |
1676 | static void warn_for_collisions (struct tlist *); | |
1677 | static void warn_for_collisions_1 (tree, tree, struct tlist *, int); | |
1678 | static struct tlist *new_tlist (struct tlist *, tree, tree); | |
481c6ce6 | 1679 | |
4e91a871 | 1680 | /* Create a new struct tlist and fill in its fields. */ |
1681 | static struct tlist * | |
1cae46be | 1682 | new_tlist (struct tlist *next, tree t, tree writer) |
4e91a871 | 1683 | { |
1684 | struct tlist *l; | |
9318f22c | 1685 | l = XOBNEW (&tlist_obstack, struct tlist); |
4e91a871 | 1686 | l->next = next; |
1687 | l->expr = t; | |
1688 | l->writer = writer; | |
1689 | return l; | |
1690 | } | |
1691 | ||
1692 | /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER | |
1693 | is nonnull, we ignore any node we find which has a writer equal to it. */ | |
1694 | ||
1695 | static void | |
1cae46be | 1696 | add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy) |
4e91a871 | 1697 | { |
1698 | while (add) | |
1699 | { | |
1700 | struct tlist *next = add->next; | |
84166705 | 1701 | if (!copy) |
4e91a871 | 1702 | add->next = *to; |
84166705 | 1703 | if (!exclude_writer || add->writer != exclude_writer) |
4e91a871 | 1704 | *to = copy ? new_tlist (*to, add->expr, add->writer) : add; |
1705 | add = next; | |
1706 | } | |
1707 | } | |
1708 | ||
1709 | /* Merge the nodes of ADD into TO. This merging process is done so that for | |
1710 | each variable that already exists in TO, no new node is added; however if | |
1711 | there is a write access recorded in ADD, and an occurrence on TO is only | |
1712 | a read access, then the occurrence in TO will be modified to record the | |
1713 | write. */ | |
481c6ce6 | 1714 | |
1715 | static void | |
1cae46be | 1716 | merge_tlist (struct tlist **to, struct tlist *add, int copy) |
4e91a871 | 1717 | { |
1718 | struct tlist **end = to; | |
1719 | ||
1720 | while (*end) | |
1721 | end = &(*end)->next; | |
1722 | ||
1723 | while (add) | |
1724 | { | |
1725 | int found = 0; | |
1726 | struct tlist *tmp2; | |
1727 | struct tlist *next = add->next; | |
1728 | ||
1729 | for (tmp2 = *to; tmp2; tmp2 = tmp2->next) | |
1730 | if (tmp2->expr == add->expr) | |
1731 | { | |
1732 | found = 1; | |
84166705 | 1733 | if (!tmp2->writer) |
4e91a871 | 1734 | tmp2->writer = add->writer; |
1735 | } | |
84166705 | 1736 | if (!found) |
4e91a871 | 1737 | { |
1738 | *end = copy ? add : new_tlist (NULL, add->expr, add->writer); | |
1739 | end = &(*end)->next; | |
1740 | *end = 0; | |
1741 | } | |
1742 | add = next; | |
1743 | } | |
1744 | } | |
1745 | ||
1746 | /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable | |
1747 | references in list LIST conflict with it, excluding reads if ONLY writers | |
1748 | is nonzero. */ | |
1749 | ||
1750 | static void | |
1cae46be | 1751 | warn_for_collisions_1 (tree written, tree writer, struct tlist *list, |
1752 | int only_writes) | |
4e91a871 | 1753 | { |
1754 | struct tlist *tmp; | |
1755 | ||
1756 | /* Avoid duplicate warnings. */ | |
1757 | for (tmp = warned_ids; tmp; tmp = tmp->next) | |
1758 | if (tmp->expr == written) | |
1759 | return; | |
1760 | ||
1761 | while (list) | |
1762 | { | |
1763 | if (list->expr == written | |
1764 | && list->writer != writer | |
84166705 | 1765 | && (!only_writes || list->writer) |
e4323659 | 1766 | && DECL_NAME (list->expr)) |
4e91a871 | 1767 | { |
1768 | warned_ids = new_tlist (warned_ids, written, NULL_TREE); | |
23ce1cf6 | 1769 | warning (OPT_Wsequence_point, "operation on %qE may be undefined", |
1770 | list->expr); | |
4e91a871 | 1771 | } |
1772 | list = list->next; | |
1773 | } | |
1774 | } | |
1775 | ||
1776 | /* Given a list LIST of references to variables, find whether any of these | |
1777 | can cause conflicts due to missing sequence points. */ | |
1778 | ||
1779 | static void | |
1cae46be | 1780 | warn_for_collisions (struct tlist *list) |
4e91a871 | 1781 | { |
1782 | struct tlist *tmp; | |
1cae46be | 1783 | |
4e91a871 | 1784 | for (tmp = list; tmp; tmp = tmp->next) |
1785 | { | |
1786 | if (tmp->writer) | |
1787 | warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); | |
1788 | } | |
1789 | } | |
1790 | ||
734c98be | 1791 | /* Return nonzero if X is a tree that can be verified by the sequence point |
4e91a871 | 1792 | warnings. */ |
1793 | static int | |
1cae46be | 1794 | warning_candidate_p (tree x) |
481c6ce6 | 1795 | { |
4e91a871 | 1796 | return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL; |
1797 | } | |
481c6ce6 | 1798 | |
4e91a871 | 1799 | /* Walk the tree X, and record accesses to variables. If X is written by the |
1800 | parent tree, WRITER is the parent. | |
1801 | We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this | |
1802 | expression or its only operand forces a sequence point, then everything up | |
1803 | to the sequence point is stored in PBEFORE_SP. Everything else gets stored | |
1804 | in PNO_SP. | |
1805 | Once we return, we will have emitted warnings if any subexpression before | |
1806 | such a sequence point could be undefined. On a higher level, however, the | |
1807 | sequence point may not be relevant, and we'll merge the two lists. | |
1808 | ||
1809 | Example: (b++, a) + b; | |
1810 | The call that processes the COMPOUND_EXPR will store the increment of B | |
1811 | in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that | |
1812 | processes the PLUS_EXPR will need to merge the two lists so that | |
1813 | eventually, all accesses end up on the same list (and we'll warn about the | |
1814 | unordered subexpressions b++ and b. | |
1815 | ||
1816 | A note on merging. If we modify the former example so that our expression | |
1817 | becomes | |
1818 | (b++, b) + a | |
1819 | care must be taken not simply to add all three expressions into the final | |
1820 | PNO_SP list. The function merge_tlist takes care of that by merging the | |
1821 | before-SP list of the COMPOUND_EXPR into its after-SP list in a special | |
1822 | way, so that no more than one access to B is recorded. */ | |
481c6ce6 | 1823 | |
4e91a871 | 1824 | static void |
1cae46be | 1825 | verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp, |
1826 | tree writer) | |
4e91a871 | 1827 | { |
1828 | struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; | |
1829 | enum tree_code code; | |
ce45a448 | 1830 | enum tree_code_class cl; |
481c6ce6 | 1831 | |
e5b75768 | 1832 | /* X may be NULL if it is the operand of an empty statement expression |
1833 | ({ }). */ | |
1834 | if (x == NULL) | |
1835 | return; | |
1836 | ||
4e91a871 | 1837 | restart: |
1838 | code = TREE_CODE (x); | |
e916c70c | 1839 | cl = TREE_CODE_CLASS (code); |
481c6ce6 | 1840 | |
4e91a871 | 1841 | if (warning_candidate_p (x)) |
481c6ce6 | 1842 | { |
4e91a871 | 1843 | *pno_sp = new_tlist (*pno_sp, x, writer); |
1844 | return; | |
1845 | } | |
1846 | ||
1847 | switch (code) | |
1848 | { | |
67b28e3e | 1849 | case CONSTRUCTOR: |
1850 | return; | |
1851 | ||
4e91a871 | 1852 | case COMPOUND_EXPR: |
1853 | case TRUTH_ANDIF_EXPR: | |
1854 | case TRUTH_ORIF_EXPR: | |
1855 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
1856 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
1857 | warn_for_collisions (tmp_nosp); | |
1858 | merge_tlist (pbefore_sp, tmp_before, 0); | |
1859 | merge_tlist (pbefore_sp, tmp_nosp, 0); | |
1860 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); | |
1861 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
1862 | return; | |
1863 | ||
1864 | case COND_EXPR: | |
1865 | tmp_before = tmp_list2 = 0; | |
1866 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); | |
1867 | warn_for_collisions (tmp_list2); | |
1868 | merge_tlist (pbefore_sp, tmp_before, 0); | |
1869 | merge_tlist (pbefore_sp, tmp_list2, 1); | |
1870 | ||
1871 | tmp_list3 = tmp_nosp = 0; | |
1872 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); | |
1873 | warn_for_collisions (tmp_nosp); | |
1874 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
1875 | ||
1876 | tmp_list3 = tmp_list2 = 0; | |
1877 | verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); | |
1878 | warn_for_collisions (tmp_list2); | |
1879 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
1880 | /* Rather than add both tmp_nosp and tmp_list2, we have to merge the | |
1881 | two first, to avoid warning for (a ? b++ : b++). */ | |
1882 | merge_tlist (&tmp_nosp, tmp_list2, 0); | |
1883 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
1884 | return; | |
1885 | ||
481c6ce6 | 1886 | case PREDECREMENT_EXPR: |
1887 | case PREINCREMENT_EXPR: | |
1888 | case POSTDECREMENT_EXPR: | |
1889 | case POSTINCREMENT_EXPR: | |
4e91a871 | 1890 | verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); |
1891 | return; | |
1892 | ||
1893 | case MODIFY_EXPR: | |
1894 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
1895 | verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); | |
1896 | verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); | |
1897 | /* Expressions inside the LHS are not ordered wrt. the sequence points | |
1898 | in the RHS. Example: | |
1899 | *a = (a++, 2) | |
1900 | Despite the fact that the modification of "a" is in the before_sp | |
1901 | list (tmp_before), it conflicts with the use of "a" in the LHS. | |
1902 | We can handle this by adding the contents of tmp_list3 | |
1903 | to those of tmp_before, and redoing the collision warnings for that | |
1904 | list. */ | |
1905 | add_tlist (&tmp_before, tmp_list3, x, 1); | |
1906 | warn_for_collisions (tmp_before); | |
1907 | /* Exclude the LHS itself here; we first have to merge it into the | |
1908 | tmp_nosp list. This is done to avoid warning for "a = a"; if we | |
1909 | didn't exclude the LHS, we'd get it twice, once as a read and once | |
1910 | as a write. */ | |
1911 | add_tlist (pno_sp, tmp_list3, x, 0); | |
1912 | warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); | |
1913 | ||
1914 | merge_tlist (pbefore_sp, tmp_before, 0); | |
1915 | if (warning_candidate_p (TREE_OPERAND (x, 0))) | |
1916 | merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); | |
1917 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); | |
1918 | return; | |
481c6ce6 | 1919 | |
1920 | case CALL_EXPR: | |
4e91a871 | 1921 | /* We need to warn about conflicts among arguments and conflicts between |
1922 | args and the function address. Side effects of the function address, | |
1923 | however, are not ordered by the sequence point of the call. */ | |
c2f47e15 | 1924 | { |
1925 | call_expr_arg_iterator iter; | |
1926 | tree arg; | |
1927 | tmp_before = tmp_nosp = 0; | |
1928 | verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
1929 | FOR_EACH_CALL_EXPR_ARG (arg, iter, x) | |
1930 | { | |
1931 | tmp_list2 = tmp_list3 = 0; | |
1932 | verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE); | |
1933 | merge_tlist (&tmp_list3, tmp_list2, 0); | |
1934 | add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); | |
1935 | } | |
1936 | add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); | |
1937 | warn_for_collisions (tmp_before); | |
1938 | add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); | |
1939 | return; | |
1940 | } | |
481c6ce6 | 1941 | |
1942 | case TREE_LIST: | |
1943 | /* Scan all the list, e.g. indices of multi dimensional array. */ | |
1944 | while (x) | |
1945 | { | |
4e91a871 | 1946 | tmp_before = tmp_nosp = 0; |
1947 | verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
1948 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
1949 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
481c6ce6 | 1950 | x = TREE_CHAIN (x); |
1951 | } | |
4e91a871 | 1952 | return; |
481c6ce6 | 1953 | |
4e91a871 | 1954 | case SAVE_EXPR: |
1955 | { | |
1956 | struct tlist_cache *t; | |
1957 | for (t = save_expr_cache; t; t = t->next) | |
1958 | if (t->expr == x) | |
1959 | break; | |
481c6ce6 | 1960 | |
84166705 | 1961 | if (!t) |
481c6ce6 | 1962 | { |
9318f22c | 1963 | t = XOBNEW (&tlist_obstack, struct tlist_cache); |
4e91a871 | 1964 | t->next = save_expr_cache; |
1965 | t->expr = x; | |
1966 | save_expr_cache = t; | |
1967 | ||
1968 | tmp_before = tmp_nosp = 0; | |
1969 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
1970 | warn_for_collisions (tmp_nosp); | |
1971 | ||
1972 | tmp_list3 = 0; | |
1973 | while (tmp_nosp) | |
1974 | { | |
1975 | struct tlist *t = tmp_nosp; | |
1976 | tmp_nosp = t->next; | |
1977 | merge_tlist (&tmp_list3, t, 0); | |
1978 | } | |
1979 | t->cache_before_sp = tmp_before; | |
1980 | t->cache_after_sp = tmp_list3; | |
481c6ce6 | 1981 | } |
4e91a871 | 1982 | merge_tlist (pbefore_sp, t->cache_before_sp, 1); |
1983 | add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); | |
1984 | return; | |
1985 | } | |
481c6ce6 | 1986 | |
ce45a448 | 1987 | default: |
1988 | /* For other expressions, simply recurse on their operands. | |
a0c938f0 | 1989 | Manual tail recursion for unary expressions. |
ce45a448 | 1990 | Other non-expressions need not be processed. */ |
1991 | if (cl == tcc_unary) | |
1992 | { | |
ce45a448 | 1993 | x = TREE_OPERAND (x, 0); |
1994 | writer = 0; | |
1995 | goto restart; | |
1996 | } | |
1997 | else if (IS_EXPR_CODE_CLASS (cl)) | |
1998 | { | |
1999 | int lp; | |
c2f47e15 | 2000 | int max = TREE_OPERAND_LENGTH (x); |
ce45a448 | 2001 | for (lp = 0; lp < max; lp++) |
2002 | { | |
2003 | tmp_before = tmp_nosp = 0; | |
2004 | verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0); | |
2005 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
2006 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
2007 | } | |
2008 | } | |
2009 | return; | |
481c6ce6 | 2010 | } |
481c6ce6 | 2011 | } |
2012 | ||
974e2c0c | 2013 | /* Try to warn for undefined behavior in EXPR due to missing sequence |
481c6ce6 | 2014 | points. */ |
2015 | ||
2569a1be | 2016 | void |
1cae46be | 2017 | verify_sequence_points (tree expr) |
481c6ce6 | 2018 | { |
4e91a871 | 2019 | struct tlist *before_sp = 0, *after_sp = 0; |
481c6ce6 | 2020 | |
4e91a871 | 2021 | warned_ids = 0; |
2022 | save_expr_cache = 0; | |
2023 | if (tlist_firstobj == 0) | |
481c6ce6 | 2024 | { |
4e91a871 | 2025 | gcc_obstack_init (&tlist_obstack); |
4fd61bc6 | 2026 | tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0); |
481c6ce6 | 2027 | } |
2028 | ||
4e91a871 | 2029 | verify_tree (expr, &before_sp, &after_sp, 0); |
2030 | warn_for_collisions (after_sp); | |
2031 | obstack_free (&tlist_obstack, tlist_firstobj); | |
481c6ce6 | 2032 | } |
b0fc3e72 | 2033 | \f |
2034 | /* Validate the expression after `case' and apply default promotions. */ | |
2035 | ||
2ca392fd | 2036 | static tree |
1cae46be | 2037 | check_case_value (tree value) |
b0fc3e72 | 2038 | { |
2039 | if (value == NULL_TREE) | |
2040 | return value; | |
2041 | ||
a97c952c | 2042 | /* ??? Can we ever get nops here for a valid case value? We |
2043 | shouldn't for C. */ | |
fce1d6af | 2044 | STRIP_TYPE_NOPS (value); |
225ec6aa | 2045 | /* In C++, the following is allowed: |
2046 | ||
2047 | const int i = 3; | |
2048 | switch (...) { case i: ... } | |
2049 | ||
2050 | So, we try to reduce the VALUE to a constant that way. */ | |
c0f19401 | 2051 | if (c_dialect_cxx ()) |
225ec6aa | 2052 | { |
2053 | value = decl_constant_value (value); | |
2054 | STRIP_TYPE_NOPS (value); | |
2055 | value = fold (value); | |
2056 | } | |
b0fc3e72 | 2057 | |
b96dc121 | 2058 | if (TREE_CODE (value) == INTEGER_CST) |
2059 | /* Promote char or short to int. */ | |
2060 | value = perform_integral_promotions (value); | |
2061 | else if (value != error_mark_node) | |
b0fc3e72 | 2062 | { |
2063 | error ("case label does not reduce to an integer constant"); | |
2064 | value = error_mark_node; | |
2065 | } | |
b0fc3e72 | 2066 | |
6433f1c2 | 2067 | constant_expression_warning (value); |
2068 | ||
b0fc3e72 | 2069 | return value; |
2070 | } | |
2071 | \f | |
2ca392fd | 2072 | /* See if the case values LOW and HIGH are in the range of the original |
5c9dae64 | 2073 | type (i.e. before the default conversion to int) of the switch testing |
2ca392fd | 2074 | expression. |
2075 | TYPE is the promoted type of the testing expression, and ORIG_TYPE is | |
91275768 | 2076 | the type before promoting it. CASE_LOW_P is a pointer to the lower |
2ca392fd | 2077 | bound of the case label, and CASE_HIGH_P is the upper bound or NULL |
2078 | if the case is not a case range. | |
2079 | The caller has to make sure that we are not called with NULL for | |
5c9dae64 | 2080 | CASE_LOW_P (i.e. the default case). |
442e3cb9 | 2081 | Returns true if the case label is in range of ORIG_TYPE (saturated or |
2ca392fd | 2082 | untouched) or false if the label is out of range. */ |
2083 | ||
2084 | static bool | |
2085 | check_case_bounds (tree type, tree orig_type, | |
2086 | tree *case_low_p, tree *case_high_p) | |
2087 | { | |
2088 | tree min_value, max_value; | |
2089 | tree case_low = *case_low_p; | |
2090 | tree case_high = case_high_p ? *case_high_p : case_low; | |
2091 | ||
2092 | /* If there was a problem with the original type, do nothing. */ | |
2093 | if (orig_type == error_mark_node) | |
2094 | return true; | |
2095 | ||
2096 | min_value = TYPE_MIN_VALUE (orig_type); | |
2097 | max_value = TYPE_MAX_VALUE (orig_type); | |
2098 | ||
2099 | /* Case label is less than minimum for type. */ | |
2100 | if (tree_int_cst_compare (case_low, min_value) < 0 | |
2101 | && tree_int_cst_compare (case_high, min_value) < 0) | |
2102 | { | |
c3ceba8e | 2103 | warning (0, "case label value is less than minimum value for type"); |
2ca392fd | 2104 | return false; |
2105 | } | |
b27ac6b5 | 2106 | |
2ca392fd | 2107 | /* Case value is greater than maximum for type. */ |
2108 | if (tree_int_cst_compare (case_low, max_value) > 0 | |
2109 | && tree_int_cst_compare (case_high, max_value) > 0) | |
2110 | { | |
c3ceba8e | 2111 | warning (0, "case label value exceeds maximum value for type"); |
2ca392fd | 2112 | return false; |
2113 | } | |
2114 | ||
2115 | /* Saturate lower case label value to minimum. */ | |
2116 | if (tree_int_cst_compare (case_high, min_value) >= 0 | |
2117 | && tree_int_cst_compare (case_low, min_value) < 0) | |
2118 | { | |
c3ceba8e | 2119 | warning (0, "lower value in case label range" |
2ca392fd | 2120 | " less than minimum value for type"); |
2121 | case_low = min_value; | |
2122 | } | |
b27ac6b5 | 2123 | |
2ca392fd | 2124 | /* Saturate upper case label value to maximum. */ |
2125 | if (tree_int_cst_compare (case_low, max_value) <= 0 | |
2126 | && tree_int_cst_compare (case_high, max_value) > 0) | |
2127 | { | |
c3ceba8e | 2128 | warning (0, "upper value in case label range" |
2ca392fd | 2129 | " exceeds maximum value for type"); |
2130 | case_high = max_value; | |
2131 | } | |
2132 | ||
2133 | if (*case_low_p != case_low) | |
2134 | *case_low_p = convert (type, case_low); | |
2135 | if (case_high_p && *case_high_p != case_high) | |
2136 | *case_high_p = convert (type, case_high); | |
2137 | ||
2138 | return true; | |
2139 | } | |
2140 | \f | |
b0fc3e72 | 2141 | /* Return an integer type with BITS bits of precision, |
2142 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
2143 | ||
2144 | tree | |
1cae46be | 2145 | c_common_type_for_size (unsigned int bits, int unsignedp) |
b0fc3e72 | 2146 | { |
46375237 | 2147 | if (bits == TYPE_PRECISION (integer_type_node)) |
2148 | return unsignedp ? unsigned_type_node : integer_type_node; | |
2149 | ||
bacde65a | 2150 | if (bits == TYPE_PRECISION (signed_char_type_node)) |
b0fc3e72 | 2151 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
2152 | ||
bacde65a | 2153 | if (bits == TYPE_PRECISION (short_integer_type_node)) |
b0fc3e72 | 2154 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
2155 | ||
bacde65a | 2156 | if (bits == TYPE_PRECISION (long_integer_type_node)) |
b0fc3e72 | 2157 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
2158 | ||
bacde65a | 2159 | if (bits == TYPE_PRECISION (long_long_integer_type_node)) |
b0fc3e72 | 2160 | return (unsignedp ? long_long_unsigned_type_node |
2161 | : long_long_integer_type_node); | |
2162 | ||
f57fa2ea | 2163 | if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) |
2164 | return (unsignedp ? widest_unsigned_literal_type_node | |
2165 | : widest_integer_literal_type_node); | |
2166 | ||
bacde65a | 2167 | if (bits <= TYPE_PRECISION (intQI_type_node)) |
2168 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
2169 | ||
2170 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
2171 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
2172 | ||
2173 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
2174 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
2175 | ||
2176 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
2177 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
2178 | ||
b0fc3e72 | 2179 | return 0; |
2180 | } | |
2181 | ||
9421ebb9 | 2182 | /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits |
2183 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed; | |
2184 | and saturating if SATP is nonzero, otherwise not saturating. */ | |
2185 | ||
2186 | tree | |
2187 | c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit, | |
2188 | int unsignedp, int satp) | |
2189 | { | |
2190 | enum machine_mode mode; | |
2191 | if (ibit == 0) | |
2192 | mode = unsignedp ? UQQmode : QQmode; | |
2193 | else | |
2194 | mode = unsignedp ? UHAmode : HAmode; | |
2195 | ||
2196 | for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode)) | |
2197 | if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit) | |
2198 | break; | |
2199 | ||
2200 | if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode)) | |
2201 | { | |
2202 | sorry ("GCC cannot support operators with integer types and " | |
2203 | "fixed-point types that have too many integral and " | |
2204 | "fractional bits together"); | |
2205 | return 0; | |
2206 | } | |
2207 | ||
2208 | return c_common_type_for_mode (mode, satp); | |
2209 | } | |
2210 | ||
5b247e9f | 2211 | /* Used for communication between c_common_type_for_mode and |
2212 | c_register_builtin_type. */ | |
2213 | static GTY(()) tree registered_builtin_types; | |
2214 | ||
b0fc3e72 | 2215 | /* Return a data type that has machine mode MODE. |
2216 | If the mode is an integer, | |
9421ebb9 | 2217 | then UNSIGNEDP selects between signed and unsigned types. |
2218 | If the mode is a fixed-point mode, | |
2219 | then UNSIGNEDP selects between saturating and nonsaturating types. */ | |
b0fc3e72 | 2220 | |
2221 | tree | |
1cae46be | 2222 | c_common_type_for_mode (enum machine_mode mode, int unsignedp) |
b0fc3e72 | 2223 | { |
5b247e9f | 2224 | tree t; |
2225 | ||
46375237 | 2226 | if (mode == TYPE_MODE (integer_type_node)) |
2227 | return unsignedp ? unsigned_type_node : integer_type_node; | |
2228 | ||
b0fc3e72 | 2229 | if (mode == TYPE_MODE (signed_char_type_node)) |
2230 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
2231 | ||
2232 | if (mode == TYPE_MODE (short_integer_type_node)) | |
2233 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
2234 | ||
b0fc3e72 | 2235 | if (mode == TYPE_MODE (long_integer_type_node)) |
2236 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
2237 | ||
2238 | if (mode == TYPE_MODE (long_long_integer_type_node)) | |
2239 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
2240 | ||
f57fa2ea | 2241 | if (mode == TYPE_MODE (widest_integer_literal_type_node)) |
44e9fa65 | 2242 | return unsignedp ? widest_unsigned_literal_type_node |
4ee9c684 | 2243 | : widest_integer_literal_type_node; |
f57fa2ea | 2244 | |
88ae7f04 | 2245 | if (mode == QImode) |
bacde65a | 2246 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
2247 | ||
88ae7f04 | 2248 | if (mode == HImode) |
bacde65a | 2249 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
2250 | ||
88ae7f04 | 2251 | if (mode == SImode) |
bacde65a | 2252 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
2253 | ||
88ae7f04 | 2254 | if (mode == DImode) |
bacde65a | 2255 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
2256 | ||
cc1cc1c7 | 2257 | #if HOST_BITS_PER_WIDE_INT >= 64 |
6274009c | 2258 | if (mode == TYPE_MODE (intTI_type_node)) |
2259 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
cc1cc1c7 | 2260 | #endif |
6274009c | 2261 | |
b0fc3e72 | 2262 | if (mode == TYPE_MODE (float_type_node)) |
2263 | return float_type_node; | |
2264 | ||
2265 | if (mode == TYPE_MODE (double_type_node)) | |
2266 | return double_type_node; | |
2267 | ||
2268 | if (mode == TYPE_MODE (long_double_type_node)) | |
2269 | return long_double_type_node; | |
2270 | ||
545c2bde | 2271 | if (mode == TYPE_MODE (void_type_node)) |
2272 | return void_type_node; | |
b27ac6b5 | 2273 | |
b0fc3e72 | 2274 | if (mode == TYPE_MODE (build_pointer_type (char_type_node))) |
61b9b73c | 2275 | return (unsignedp |
2276 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
2277 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 2278 | |
2279 | if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) | |
61b9b73c | 2280 | return (unsignedp |
2281 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
2282 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 2283 | |
0dfc45b5 | 2284 | if (COMPLEX_MODE_P (mode)) |
2285 | { | |
2286 | enum machine_mode inner_mode; | |
2287 | tree inner_type; | |
2288 | ||
2289 | if (mode == TYPE_MODE (complex_float_type_node)) | |
2290 | return complex_float_type_node; | |
2291 | if (mode == TYPE_MODE (complex_double_type_node)) | |
2292 | return complex_double_type_node; | |
2293 | if (mode == TYPE_MODE (complex_long_double_type_node)) | |
2294 | return complex_long_double_type_node; | |
2295 | ||
2296 | if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp) | |
2297 | return complex_integer_type_node; | |
2298 | ||
2299 | inner_mode = GET_MODE_INNER (mode); | |
2300 | inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
2301 | if (inner_type != NULL_TREE) | |
2302 | return build_complex_type (inner_type); | |
2303 | } | |
2304 | else if (VECTOR_MODE_P (mode)) | |
4917c376 | 2305 | { |
2306 | enum machine_mode inner_mode = GET_MODE_INNER (mode); | |
2307 | tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
2308 | if (inner_type != NULL_TREE) | |
2309 | return build_vector_type_for_mode (inner_type, mode); | |
88ae7f04 | 2310 | } |
e2ea7e3a | 2311 | |
c4503c0a | 2312 | if (mode == TYPE_MODE (dfloat32_type_node)) |
2313 | return dfloat32_type_node; | |
2314 | if (mode == TYPE_MODE (dfloat64_type_node)) | |
2315 | return dfloat64_type_node; | |
2316 | if (mode == TYPE_MODE (dfloat128_type_node)) | |
2317 | return dfloat128_type_node; | |
2318 | ||
9421ebb9 | 2319 | if (ALL_SCALAR_FIXED_POINT_MODE_P (mode)) |
2320 | { | |
2321 | if (mode == TYPE_MODE (short_fract_type_node)) | |
2322 | return unsignedp ? sat_short_fract_type_node : short_fract_type_node; | |
2323 | if (mode == TYPE_MODE (fract_type_node)) | |
2324 | return unsignedp ? sat_fract_type_node : fract_type_node; | |
2325 | if (mode == TYPE_MODE (long_fract_type_node)) | |
2326 | return unsignedp ? sat_long_fract_type_node : long_fract_type_node; | |
2327 | if (mode == TYPE_MODE (long_long_fract_type_node)) | |
2328 | return unsignedp ? sat_long_long_fract_type_node | |
2329 | : long_long_fract_type_node; | |
2330 | ||
2331 | if (mode == TYPE_MODE (unsigned_short_fract_type_node)) | |
2332 | return unsignedp ? sat_unsigned_short_fract_type_node | |
2333 | : unsigned_short_fract_type_node; | |
2334 | if (mode == TYPE_MODE (unsigned_fract_type_node)) | |
2335 | return unsignedp ? sat_unsigned_fract_type_node | |
2336 | : unsigned_fract_type_node; | |
2337 | if (mode == TYPE_MODE (unsigned_long_fract_type_node)) | |
2338 | return unsignedp ? sat_unsigned_long_fract_type_node | |
2339 | : unsigned_long_fract_type_node; | |
2340 | if (mode == TYPE_MODE (unsigned_long_long_fract_type_node)) | |
2341 | return unsignedp ? sat_unsigned_long_long_fract_type_node | |
2342 | : unsigned_long_long_fract_type_node; | |
2343 | ||
2344 | if (mode == TYPE_MODE (short_accum_type_node)) | |
2345 | return unsignedp ? sat_short_accum_type_node : short_accum_type_node; | |
2346 | if (mode == TYPE_MODE (accum_type_node)) | |
2347 | return unsignedp ? sat_accum_type_node : accum_type_node; | |
2348 | if (mode == TYPE_MODE (long_accum_type_node)) | |
2349 | return unsignedp ? sat_long_accum_type_node : long_accum_type_node; | |
2350 | if (mode == TYPE_MODE (long_long_accum_type_node)) | |
2351 | return unsignedp ? sat_long_long_accum_type_node | |
2352 | : long_long_accum_type_node; | |
2353 | ||
2354 | if (mode == TYPE_MODE (unsigned_short_accum_type_node)) | |
2355 | return unsignedp ? sat_unsigned_short_accum_type_node | |
2356 | : unsigned_short_accum_type_node; | |
2357 | if (mode == TYPE_MODE (unsigned_accum_type_node)) | |
2358 | return unsignedp ? sat_unsigned_accum_type_node | |
2359 | : unsigned_accum_type_node; | |
2360 | if (mode == TYPE_MODE (unsigned_long_accum_type_node)) | |
2361 | return unsignedp ? sat_unsigned_long_accum_type_node | |
2362 | : unsigned_long_accum_type_node; | |
2363 | if (mode == TYPE_MODE (unsigned_long_long_accum_type_node)) | |
2364 | return unsignedp ? sat_unsigned_long_long_accum_type_node | |
2365 | : unsigned_long_long_accum_type_node; | |
2366 | ||
2367 | if (mode == QQmode) | |
2368 | return unsignedp ? sat_qq_type_node : qq_type_node; | |
2369 | if (mode == HQmode) | |
2370 | return unsignedp ? sat_hq_type_node : hq_type_node; | |
2371 | if (mode == SQmode) | |
2372 | return unsignedp ? sat_sq_type_node : sq_type_node; | |
2373 | if (mode == DQmode) | |
2374 | return unsignedp ? sat_dq_type_node : dq_type_node; | |
2375 | if (mode == TQmode) | |
2376 | return unsignedp ? sat_tq_type_node : tq_type_node; | |
2377 | ||
2378 | if (mode == UQQmode) | |
2379 | return unsignedp ? sat_uqq_type_node : uqq_type_node; | |
2380 | if (mode == UHQmode) | |
2381 | return unsignedp ? sat_uhq_type_node : uhq_type_node; | |
2382 | if (mode == USQmode) | |
2383 | return unsignedp ? sat_usq_type_node : usq_type_node; | |
2384 | if (mode == UDQmode) | |
2385 | return unsignedp ? sat_udq_type_node : udq_type_node; | |
2386 | if (mode == UTQmode) | |
2387 | return unsignedp ? sat_utq_type_node : utq_type_node; | |
2388 | ||
2389 | if (mode == HAmode) | |
2390 | return unsignedp ? sat_ha_type_node : ha_type_node; | |
2391 | if (mode == SAmode) | |
2392 | return unsignedp ? sat_sa_type_node : sa_type_node; | |
2393 | if (mode == DAmode) | |
2394 | return unsignedp ? sat_da_type_node : da_type_node; | |
2395 | if (mode == TAmode) | |
2396 | return unsignedp ? sat_ta_type_node : ta_type_node; | |
2397 | ||
2398 | if (mode == UHAmode) | |
2399 | return unsignedp ? sat_uha_type_node : uha_type_node; | |
2400 | if (mode == USAmode) | |
2401 | return unsignedp ? sat_usa_type_node : usa_type_node; | |
2402 | if (mode == UDAmode) | |
2403 | return unsignedp ? sat_uda_type_node : uda_type_node; | |
2404 | if (mode == UTAmode) | |
2405 | return unsignedp ? sat_uta_type_node : uta_type_node; | |
2406 | } | |
2407 | ||
5b247e9f | 2408 | for (t = registered_builtin_types; t; t = TREE_CHAIN (t)) |
2409 | if (TYPE_MODE (TREE_VALUE (t)) == mode) | |
2410 | return TREE_VALUE (t); | |
2411 | ||
b0fc3e72 | 2412 | return 0; |
2413 | } | |
20d39783 | 2414 | |
11773141 | 2415 | tree |
2416 | c_common_unsigned_type (tree type) | |
2417 | { | |
2418 | return c_common_signed_or_unsigned_type (1, type); | |
2419 | } | |
2420 | ||
20d39783 | 2421 | /* Return a signed type the same as TYPE in other respects. */ |
2422 | ||
2423 | tree | |
1cae46be | 2424 | c_common_signed_type (tree type) |
20d39783 | 2425 | { |
4070745f | 2426 | return c_common_signed_or_unsigned_type (0, type); |
20d39783 | 2427 | } |
2428 | ||
2429 | /* Return a type the same as TYPE except unsigned or | |
2430 | signed according to UNSIGNEDP. */ | |
2431 | ||
2432 | tree | |
1cae46be | 2433 | c_common_signed_or_unsigned_type (int unsignedp, tree type) |
20d39783 | 2434 | { |
7a91101f | 2435 | tree type1; |
20d39783 | 2436 | |
7a91101f | 2437 | /* This block of code emulates the behavior of the old |
2438 | c_common_unsigned_type. In particular, it returns | |
2439 | long_unsigned_type_node if passed a long, even when a int would | |
2440 | have the same size. This is necessary for warnings to work | |
2441 | correctly in archs where sizeof(int) == sizeof(long) */ | |
2442 | ||
2443 | type1 = TYPE_MAIN_VARIANT (type); | |
2444 | if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node) | |
2445 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
2446 | if (type1 == integer_type_node || type1 == unsigned_type_node) | |
2447 | return unsignedp ? unsigned_type_node : integer_type_node; | |
2448 | if (type1 == short_integer_type_node || type1 == short_unsigned_type_node) | |
2449 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
2450 | if (type1 == long_integer_type_node || type1 == long_unsigned_type_node) | |
2451 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
2452 | if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node) | |
2453 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
2454 | if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node) | |
2455 | return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node; | |
2456 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
2457 | if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node) | |
2458 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
2459 | #endif | |
2460 | if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node) | |
2461 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
2462 | if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node) | |
2463 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
2464 | if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node) | |
2465 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
2466 | if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node) | |
2467 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
2468 | ||
9f92e1a2 | 2469 | #define C_COMMON_FIXED_TYPES(NAME) \ |
2470 | if (type1 == short_ ## NAME ## _type_node \ | |
2471 | || type1 == unsigned_short_ ## NAME ## _type_node) \ | |
2472 | return unsignedp ? unsigned_short_ ## NAME ## _type_node \ | |
2473 | : short_ ## NAME ## _type_node; \ | |
2474 | if (type1 == NAME ## _type_node \ | |
2475 | || type1 == unsigned_ ## NAME ## _type_node) \ | |
2476 | return unsignedp ? unsigned_ ## NAME ## _type_node \ | |
2477 | : NAME ## _type_node; \ | |
2478 | if (type1 == long_ ## NAME ## _type_node \ | |
2479 | || type1 == unsigned_long_ ## NAME ## _type_node) \ | |
2480 | return unsignedp ? unsigned_long_ ## NAME ## _type_node \ | |
2481 | : long_ ## NAME ## _type_node; \ | |
2482 | if (type1 == long_long_ ## NAME ## _type_node \ | |
2483 | || type1 == unsigned_long_long_ ## NAME ## _type_node) \ | |
2484 | return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \ | |
2485 | : long_long_ ## NAME ## _type_node; | |
2486 | ||
2487 | #define C_COMMON_FIXED_MODE_TYPES(NAME) \ | |
2488 | if (type1 == NAME ## _type_node \ | |
2489 | || type1 == u ## NAME ## _type_node) \ | |
2490 | return unsignedp ? u ## NAME ## _type_node \ | |
2491 | : NAME ## _type_node; | |
2492 | ||
2493 | #define C_COMMON_FIXED_TYPES_SAT(NAME) \ | |
2494 | if (type1 == sat_ ## short_ ## NAME ## _type_node \ | |
2495 | || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \ | |
2496 | return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \ | |
2497 | : sat_ ## short_ ## NAME ## _type_node; \ | |
2498 | if (type1 == sat_ ## NAME ## _type_node \ | |
2499 | || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \ | |
2500 | return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \ | |
2501 | : sat_ ## NAME ## _type_node; \ | |
2502 | if (type1 == sat_ ## long_ ## NAME ## _type_node \ | |
2503 | || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \ | |
2504 | return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \ | |
2505 | : sat_ ## long_ ## NAME ## _type_node; \ | |
2506 | if (type1 == sat_ ## long_long_ ## NAME ## _type_node \ | |
2507 | || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \ | |
2508 | return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \ | |
2509 | : sat_ ## long_long_ ## NAME ## _type_node; | |
2510 | ||
2511 | #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \ | |
2512 | if (type1 == sat_ ## NAME ## _type_node \ | |
2513 | || type1 == sat_ ## u ## NAME ## _type_node) \ | |
2514 | return unsignedp ? sat_ ## u ## NAME ## _type_node \ | |
2515 | : sat_ ## NAME ## _type_node; | |
2516 | ||
2517 | C_COMMON_FIXED_TYPES (fract); | |
2518 | C_COMMON_FIXED_TYPES_SAT (fract); | |
2519 | C_COMMON_FIXED_TYPES (accum); | |
2520 | C_COMMON_FIXED_TYPES_SAT (accum); | |
2521 | ||
2522 | C_COMMON_FIXED_MODE_TYPES (qq); | |
2523 | C_COMMON_FIXED_MODE_TYPES (hq); | |
2524 | C_COMMON_FIXED_MODE_TYPES (sq); | |
2525 | C_COMMON_FIXED_MODE_TYPES (dq); | |
2526 | C_COMMON_FIXED_MODE_TYPES (tq); | |
2527 | C_COMMON_FIXED_MODE_TYPES_SAT (qq); | |
2528 | C_COMMON_FIXED_MODE_TYPES_SAT (hq); | |
2529 | C_COMMON_FIXED_MODE_TYPES_SAT (sq); | |
2530 | C_COMMON_FIXED_MODE_TYPES_SAT (dq); | |
2531 | C_COMMON_FIXED_MODE_TYPES_SAT (tq); | |
2532 | C_COMMON_FIXED_MODE_TYPES (ha); | |
2533 | C_COMMON_FIXED_MODE_TYPES (sa); | |
2534 | C_COMMON_FIXED_MODE_TYPES (da); | |
2535 | C_COMMON_FIXED_MODE_TYPES (ta); | |
2536 | C_COMMON_FIXED_MODE_TYPES_SAT (ha); | |
2537 | C_COMMON_FIXED_MODE_TYPES_SAT (sa); | |
2538 | C_COMMON_FIXED_MODE_TYPES_SAT (da); | |
2539 | C_COMMON_FIXED_MODE_TYPES_SAT (ta); | |
9421ebb9 | 2540 | |
4f7f7efd | 2541 | /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not |
2542 | the precision; they have precision set to match their range, but | |
2543 | may use a wider mode to match an ABI. If we change modes, we may | |
2544 | wind up with bad conversions. For INTEGER_TYPEs in C, must check | |
2545 | the precision as well, so as to yield correct results for | |
2546 | bit-field types. C++ does not have these separate bit-field | |
2547 | types, and producing a signed or unsigned variant of an | |
2548 | ENUMERAL_TYPE may cause other problems as well. */ | |
2549 | ||
ac265864 | 2550 | if (!INTEGRAL_TYPE_P (type) |
2551 | || TYPE_UNSIGNED (type) == unsignedp) | |
2552 | return type; | |
2553 | ||
4f7f7efd | 2554 | #define TYPE_OK(node) \ |
2555 | (TYPE_MODE (type) == TYPE_MODE (node) \ | |
2556 | && (c_dialect_cxx () || TYPE_PRECISION (type) == TYPE_PRECISION (node))) | |
2557 | if (TYPE_OK (signed_char_type_node)) | |
20d39783 | 2558 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
4f7f7efd | 2559 | if (TYPE_OK (integer_type_node)) |
20d39783 | 2560 | return unsignedp ? unsigned_type_node : integer_type_node; |
4f7f7efd | 2561 | if (TYPE_OK (short_integer_type_node)) |
20d39783 | 2562 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
4f7f7efd | 2563 | if (TYPE_OK (long_integer_type_node)) |
20d39783 | 2564 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
4f7f7efd | 2565 | if (TYPE_OK (long_long_integer_type_node)) |
20d39783 | 2566 | return (unsignedp ? long_long_unsigned_type_node |
2567 | : long_long_integer_type_node); | |
4f7f7efd | 2568 | if (TYPE_OK (widest_integer_literal_type_node)) |
20d39783 | 2569 | return (unsignedp ? widest_unsigned_literal_type_node |
2570 | : widest_integer_literal_type_node); | |
ef11801e | 2571 | |
2572 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
4f7f7efd | 2573 | if (TYPE_OK (intTI_type_node)) |
ef11801e | 2574 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; |
2575 | #endif | |
4f7f7efd | 2576 | if (TYPE_OK (intDI_type_node)) |
ef11801e | 2577 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
4f7f7efd | 2578 | if (TYPE_OK (intSI_type_node)) |
ef11801e | 2579 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
4f7f7efd | 2580 | if (TYPE_OK (intHI_type_node)) |
ef11801e | 2581 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
4f7f7efd | 2582 | if (TYPE_OK (intQI_type_node)) |
ef11801e | 2583 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
4f7f7efd | 2584 | #undef TYPE_OK |
ef11801e | 2585 | |
4f7f7efd | 2586 | if (c_dialect_cxx ()) |
2587 | return type; | |
2588 | else | |
2589 | return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp); | |
20d39783 | 2590 | } |
b268e47e | 2591 | |
c0e47fd4 | 2592 | /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */ |
2593 | ||
2594 | tree | |
2595 | c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp) | |
2596 | { | |
2597 | /* Extended integer types of the same width as a standard type have | |
2598 | lesser rank, so those of the same width as int promote to int or | |
2599 | unsigned int and are valid for printf formats expecting int or | |
2600 | unsigned int. To avoid such special cases, avoid creating | |
2601 | extended integer types for bit-fields if a standard integer type | |
2602 | is available. */ | |
2603 | if (width == TYPE_PRECISION (integer_type_node)) | |
2604 | return unsignedp ? unsigned_type_node : integer_type_node; | |
2605 | if (width == TYPE_PRECISION (signed_char_type_node)) | |
2606 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
2607 | if (width == TYPE_PRECISION (short_integer_type_node)) | |
2608 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
2609 | if (width == TYPE_PRECISION (long_integer_type_node)) | |
2610 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
2611 | if (width == TYPE_PRECISION (long_long_integer_type_node)) | |
2612 | return (unsignedp ? long_long_unsigned_type_node | |
2613 | : long_long_integer_type_node); | |
2614 | return build_nonstandard_integer_type (width, unsignedp); | |
2615 | } | |
2616 | ||
b268e47e | 2617 | /* The C version of the register_builtin_type langhook. */ |
2618 | ||
2619 | void | |
2620 | c_register_builtin_type (tree type, const char* name) | |
2621 | { | |
2622 | tree decl; | |
2623 | ||
2624 | decl = build_decl (TYPE_DECL, get_identifier (name), type); | |
2625 | DECL_ARTIFICIAL (decl) = 1; | |
2626 | if (!TYPE_NAME (type)) | |
2627 | TYPE_NAME (type) = decl; | |
2628 | pushdecl (decl); | |
5b247e9f | 2629 | |
2630 | registered_builtin_types = tree_cons (0, type, registered_builtin_types); | |
b268e47e | 2631 | } |
2632 | ||
b0fc3e72 | 2633 | \f |
a9b9d10c | 2634 | /* Return the minimum number of bits needed to represent VALUE in a |
2635 | signed or unsigned type, UNSIGNEDP says which. */ | |
2636 | ||
a0c2c45b | 2637 | unsigned int |
1cae46be | 2638 | min_precision (tree value, int unsignedp) |
a9b9d10c | 2639 | { |
2640 | int log; | |
2641 | ||
2642 | /* If the value is negative, compute its negative minus 1. The latter | |
2643 | adjustment is because the absolute value of the largest negative value | |
2644 | is one larger than the largest positive value. This is equivalent to | |
2645 | a bit-wise negation, so use that operation instead. */ | |
2646 | ||
2647 | if (tree_int_cst_sgn (value) < 0) | |
49d00087 | 2648 | value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value); |
a9b9d10c | 2649 | |
2650 | /* Return the number of bits needed, taking into account the fact | |
2651 | that we need one more bit for a signed than unsigned type. */ | |
2652 | ||
2653 | if (integer_zerop (value)) | |
2654 | log = 0; | |
a9b9d10c | 2655 | else |
a0c2c45b | 2656 | log = tree_floor_log2 (value); |
a9b9d10c | 2657 | |
84166705 | 2658 | return log + 1 + !unsignedp; |
a9b9d10c | 2659 | } |
2660 | \f | |
aff9e656 | 2661 | /* Print an error message for invalid operands to arith operation |
17075f13 | 2662 | CODE with TYPE0 for operand 0, and TYPE1 for operand 1. */ |
b0fc3e72 | 2663 | |
2664 | void | |
17075f13 | 2665 | binary_op_error (enum tree_code code, tree type0, tree type1) |
b0fc3e72 | 2666 | { |
19cb6b50 | 2667 | const char *opname; |
f03946e4 | 2668 | |
b0fc3e72 | 2669 | switch (code) |
2670 | { | |
b0fc3e72 | 2671 | case PLUS_EXPR: |
2672 | opname = "+"; break; | |
2673 | case MINUS_EXPR: | |
2674 | opname = "-"; break; | |
2675 | case MULT_EXPR: | |
2676 | opname = "*"; break; | |
2677 | case MAX_EXPR: | |
2678 | opname = "max"; break; | |
2679 | case MIN_EXPR: | |
2680 | opname = "min"; break; | |
2681 | case EQ_EXPR: | |
2682 | opname = "=="; break; | |
2683 | case NE_EXPR: | |
2684 | opname = "!="; break; | |
2685 | case LE_EXPR: | |
2686 | opname = "<="; break; | |
2687 | case GE_EXPR: | |
2688 | opname = ">="; break; | |
2689 | case LT_EXPR: | |
2690 | opname = "<"; break; | |
2691 | case GT_EXPR: | |
2692 | opname = ">"; break; | |
2693 | case LSHIFT_EXPR: | |
2694 | opname = "<<"; break; | |
2695 | case RSHIFT_EXPR: | |
2696 | opname = ">>"; break; | |
2697 | case TRUNC_MOD_EXPR: | |
66618a1e | 2698 | case FLOOR_MOD_EXPR: |
b0fc3e72 | 2699 | opname = "%"; break; |
2700 | case TRUNC_DIV_EXPR: | |
66618a1e | 2701 | case FLOOR_DIV_EXPR: |
b0fc3e72 | 2702 | opname = "/"; break; |
2703 | case BIT_AND_EXPR: | |
2704 | opname = "&"; break; | |
2705 | case BIT_IOR_EXPR: | |
2706 | opname = "|"; break; | |
2707 | case TRUTH_ANDIF_EXPR: | |
2708 | opname = "&&"; break; | |
2709 | case TRUTH_ORIF_EXPR: | |
2710 | opname = "||"; break; | |
2711 | case BIT_XOR_EXPR: | |
2712 | opname = "^"; break; | |
31f820d2 | 2713 | default: |
315ba355 | 2714 | gcc_unreachable (); |
b0fc3e72 | 2715 | } |
17075f13 | 2716 | error ("invalid operands to binary %s (have %qT and %qT)", opname, |
2717 | type0, type1); | |
b0fc3e72 | 2718 | } |
2719 | \f | |
2720 | /* Subroutine of build_binary_op, used for comparison operations. | |
2721 | See if the operands have both been converted from subword integer types | |
2722 | and, if so, perhaps change them both back to their original type. | |
5b511807 | 2723 | This function is also responsible for converting the two operands |
2724 | to the proper common type for comparison. | |
b0fc3e72 | 2725 | |
2726 | The arguments of this function are all pointers to local variables | |
2727 | of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, | |
2728 | RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. | |
2729 | ||
2730 | If this function returns nonzero, it means that the comparison has | |
2731 | a constant value. What this function returns is an expression for | |
2732 | that value. */ | |
2733 | ||
2734 | tree | |
1cae46be | 2735 | shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr, |
2736 | enum tree_code *rescode_ptr) | |
b0fc3e72 | 2737 | { |
19cb6b50 | 2738 | tree type; |
b0fc3e72 | 2739 | tree op0 = *op0_ptr; |
2740 | tree op1 = *op1_ptr; | |
2741 | int unsignedp0, unsignedp1; | |
2742 | int real1, real2; | |
2743 | tree primop0, primop1; | |
2744 | enum tree_code code = *rescode_ptr; | |
2745 | ||
2746 | /* Throw away any conversions to wider types | |
2747 | already present in the operands. */ | |
2748 | ||
2749 | primop0 = get_narrower (op0, &unsignedp0); | |
2750 | primop1 = get_narrower (op1, &unsignedp1); | |
2751 | ||
2752 | /* Handle the case that OP0 does not *contain* a conversion | |
2753 | but it *requires* conversion to FINAL_TYPE. */ | |
2754 | ||
2755 | if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) | |
78a8ed03 | 2756 | unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
b0fc3e72 | 2757 | if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) |
78a8ed03 | 2758 | unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1)); |
b0fc3e72 | 2759 | |
2760 | /* If one of the operands must be floated, we cannot optimize. */ | |
2761 | real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; | |
2762 | real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; | |
2763 | ||
2764 | /* If first arg is constant, swap the args (changing operation | |
2bd278cc | 2765 | so value is preserved), for canonicalization. Don't do this if |
2766 | the second arg is 0. */ | |
b0fc3e72 | 2767 | |
2bd278cc | 2768 | if (TREE_CONSTANT (primop0) |
9421ebb9 | 2769 | && !integer_zerop (primop1) && !real_zerop (primop1) |
2770 | && !fixed_zerop (primop1)) | |
b0fc3e72 | 2771 | { |
19cb6b50 | 2772 | tree tem = primop0; |
2773 | int temi = unsignedp0; | |
b0fc3e72 | 2774 | primop0 = primop1; |
2775 | primop1 = tem; | |
2776 | tem = op0; | |
2777 | op0 = op1; | |
2778 | op1 = tem; | |
2779 | *op0_ptr = op0; | |
2780 | *op1_ptr = op1; | |
2781 | unsignedp0 = unsignedp1; | |
2782 | unsignedp1 = temi; | |
2783 | temi = real1; | |
2784 | real1 = real2; | |
2785 | real2 = temi; | |
2786 | ||
2787 | switch (code) | |
2788 | { | |
2789 | case LT_EXPR: | |
2790 | code = GT_EXPR; | |
2791 | break; | |
2792 | case GT_EXPR: | |
2793 | code = LT_EXPR; | |
2794 | break; | |
2795 | case LE_EXPR: | |
2796 | code = GE_EXPR; | |
2797 | break; | |
2798 | case GE_EXPR: | |
2799 | code = LE_EXPR; | |
2800 | break; | |
31f820d2 | 2801 | default: |
2802 | break; | |
b0fc3e72 | 2803 | } |
2804 | *rescode_ptr = code; | |
2805 | } | |
2806 | ||
2807 | /* If comparing an integer against a constant more bits wide, | |
2808 | maybe we can deduce a value of 1 or 0 independent of the data. | |
2809 | Or else truncate the constant now | |
2810 | rather than extend the variable at run time. | |
2811 | ||
2812 | This is only interesting if the constant is the wider arg. | |
2813 | Also, it is not safe if the constant is unsigned and the | |
2814 | variable arg is signed, since in this case the variable | |
2815 | would be sign-extended and then regarded as unsigned. | |
2816 | Our technique fails in this case because the lowest/highest | |
2817 | possible unsigned results don't follow naturally from the | |
2818 | lowest/highest possible values of the variable operand. | |
2819 | For just EQ_EXPR and NE_EXPR there is another technique that | |
2820 | could be used: see if the constant can be faithfully represented | |
2821 | in the other operand's type, by truncating it and reextending it | |
2822 | and see if that preserves the constant's value. */ | |
2823 | ||
2824 | if (!real1 && !real2 | |
9421ebb9 | 2825 | && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE |
b0fc3e72 | 2826 | && TREE_CODE (primop1) == INTEGER_CST |
2827 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) | |
2828 | { | |
2829 | int min_gt, max_gt, min_lt, max_lt; | |
2830 | tree maxval, minval; | |
2831 | /* 1 if comparison is nominally unsigned. */ | |
78a8ed03 | 2832 | int unsignedp = TYPE_UNSIGNED (*restype_ptr); |
b0fc3e72 | 2833 | tree val; |
2834 | ||
4070745f | 2835 | type = c_common_signed_or_unsigned_type (unsignedp0, |
2836 | TREE_TYPE (primop0)); | |
cda09c61 | 2837 | |
b0fc3e72 | 2838 | maxval = TYPE_MAX_VALUE (type); |
2839 | minval = TYPE_MIN_VALUE (type); | |
2840 | ||
2841 | if (unsignedp && !unsignedp0) | |
4070745f | 2842 | *restype_ptr = c_common_signed_type (*restype_ptr); |
b0fc3e72 | 2843 | |
2844 | if (TREE_TYPE (primop1) != *restype_ptr) | |
18dbec6f | 2845 | { |
9a5e8086 | 2846 | /* Convert primop1 to target type, but do not introduce |
2847 | additional overflow. We know primop1 is an int_cst. */ | |
c8110c8f | 2848 | primop1 = force_fit_type_double (*restype_ptr, |
2849 | TREE_INT_CST_LOW (primop1), | |
2850 | TREE_INT_CST_HIGH (primop1), 0, | |
eddad94a | 2851 | TREE_OVERFLOW (primop1)); |
18dbec6f | 2852 | } |
b0fc3e72 | 2853 | if (type != *restype_ptr) |
2854 | { | |
2855 | minval = convert (*restype_ptr, minval); | |
2856 | maxval = convert (*restype_ptr, maxval); | |
2857 | } | |
2858 | ||
2859 | if (unsignedp && unsignedp0) | |
2860 | { | |
2861 | min_gt = INT_CST_LT_UNSIGNED (primop1, minval); | |
2862 | max_gt = INT_CST_LT_UNSIGNED (primop1, maxval); | |
2863 | min_lt = INT_CST_LT_UNSIGNED (minval, primop1); | |
2864 | max_lt = INT_CST_LT_UNSIGNED (maxval, primop1); | |
2865 | } | |
2866 | else | |
2867 | { | |
2868 | min_gt = INT_CST_LT (primop1, minval); | |
2869 | max_gt = INT_CST_LT (primop1, maxval); | |
2870 | min_lt = INT_CST_LT (minval, primop1); | |
2871 | max_lt = INT_CST_LT (maxval, primop1); | |
2872 | } | |
2873 | ||
2874 | val = 0; | |
2875 | /* This used to be a switch, but Genix compiler can't handle that. */ | |
2876 | if (code == NE_EXPR) | |
2877 | { | |
2878 | if (max_lt || min_gt) | |
3c2239cf | 2879 | val = truthvalue_true_node; |
b0fc3e72 | 2880 | } |
2881 | else if (code == EQ_EXPR) | |
2882 | { | |
2883 | if (max_lt || min_gt) | |
3c2239cf | 2884 | val = truthvalue_false_node; |
b0fc3e72 | 2885 | } |
2886 | else if (code == LT_EXPR) | |
2887 | { | |
2888 | if (max_lt) | |
3c2239cf | 2889 | val = truthvalue_true_node; |
b0fc3e72 | 2890 | if (!min_lt) |
3c2239cf | 2891 | val = truthvalue_false_node; |
b0fc3e72 | 2892 | } |
2893 | else if (code == GT_EXPR) | |
2894 | { | |
2895 | if (min_gt) | |
3c2239cf | 2896 | val = truthvalue_true_node; |
b0fc3e72 | 2897 | if (!max_gt) |
3c2239cf | 2898 | val = truthvalue_false_node; |
b0fc3e72 | 2899 | } |
2900 | else if (code == LE_EXPR) | |
2901 | { | |
2902 | if (!max_gt) | |
3c2239cf | 2903 | val = truthvalue_true_node; |
b0fc3e72 | 2904 | if (min_gt) |
3c2239cf | 2905 | val = truthvalue_false_node; |
b0fc3e72 | 2906 | } |
2907 | else if (code == GE_EXPR) | |
2908 | { | |
2909 | if (!min_lt) | |
3c2239cf | 2910 | val = truthvalue_true_node; |
b0fc3e72 | 2911 | if (max_lt) |
3c2239cf | 2912 | val = truthvalue_false_node; |
b0fc3e72 | 2913 | } |
2914 | ||
2915 | /* If primop0 was sign-extended and unsigned comparison specd, | |
2916 | we did a signed comparison above using the signed type bounds. | |
2917 | But the comparison we output must be unsigned. | |
2918 | ||
2919 | Also, for inequalities, VAL is no good; but if the signed | |
2920 | comparison had *any* fixed result, it follows that the | |
2921 | unsigned comparison just tests the sign in reverse | |
2922 | (positive values are LE, negative ones GE). | |
2923 | So we can generate an unsigned comparison | |
2924 | against an extreme value of the signed type. */ | |
2925 | ||
2926 | if (unsignedp && !unsignedp0) | |
2927 | { | |
2928 | if (val != 0) | |
2929 | switch (code) | |
2930 | { | |
2931 | case LT_EXPR: | |
2932 | case GE_EXPR: | |
2933 | primop1 = TYPE_MIN_VALUE (type); | |
2934 | val = 0; | |
2935 | break; | |
2936 | ||
2937 | case LE_EXPR: | |
2938 | case GT_EXPR: | |
2939 | primop1 = TYPE_MAX_VALUE (type); | |
2940 | val = 0; | |
2941 | break; | |
31f820d2 | 2942 | |
2943 | default: | |
2944 | break; | |
b0fc3e72 | 2945 | } |
11773141 | 2946 | type = c_common_unsigned_type (type); |
b0fc3e72 | 2947 | } |
2948 | ||
fe5f2366 | 2949 | if (TREE_CODE (primop0) != INTEGER_CST) |
b0fc3e72 | 2950 | { |
3c2239cf | 2951 | if (val == truthvalue_false_node) |
e6e352cb | 2952 | warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type"); |
3c2239cf | 2953 | if (val == truthvalue_true_node) |
e6e352cb | 2954 | warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type"); |
b0fc3e72 | 2955 | } |
2956 | ||
2957 | if (val != 0) | |
2958 | { | |
2959 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
2960 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 2961 | return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); |
b0fc3e72 | 2962 | return val; |
2963 | } | |
2964 | ||
2965 | /* Value is not predetermined, but do the comparison | |
2966 | in the type of the operand that is not constant. | |
2967 | TYPE is already properly set. */ | |
2968 | } | |
c4503c0a | 2969 | |
2970 | /* If either arg is decimal float and the other is float, find the | |
2971 | proper common type to use for comparison. */ | |
2972 | else if (real1 && real2 | |
2973 | && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0))) | |
2974 | || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))) | |
2975 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
2976 | ||
b0fc3e72 | 2977 | else if (real1 && real2 |
2203bd5c | 2978 | && (TYPE_PRECISION (TREE_TYPE (primop0)) |
2979 | == TYPE_PRECISION (TREE_TYPE (primop1)))) | |
b0fc3e72 | 2980 | type = TREE_TYPE (primop0); |
2981 | ||
2982 | /* If args' natural types are both narrower than nominal type | |
2983 | and both extend in the same manner, compare them | |
2984 | in the type of the wider arg. | |
2985 | Otherwise must actually extend both to the nominal | |
2986 | common type lest different ways of extending | |
2987 | alter the result. | |
2988 | (eg, (short)-1 == (unsigned short)-1 should be 0.) */ | |
2989 | ||
2990 | else if (unsignedp0 == unsignedp1 && real1 == real2 | |
2991 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) | |
2992 | && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) | |
2993 | { | |
2994 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4070745f | 2995 | type = c_common_signed_or_unsigned_type (unsignedp0 |
78a8ed03 | 2996 | || TYPE_UNSIGNED (*restype_ptr), |
4070745f | 2997 | type); |
b0fc3e72 | 2998 | /* Make sure shorter operand is extended the right way |
2999 | to match the longer operand. */ | |
4070745f | 3000 | primop0 |
3001 | = convert (c_common_signed_or_unsigned_type (unsignedp0, | |
3002 | TREE_TYPE (primop0)), | |
3003 | primop0); | |
3004 | primop1 | |
3005 | = convert (c_common_signed_or_unsigned_type (unsignedp1, | |
3006 | TREE_TYPE (primop1)), | |
3007 | primop1); | |
b0fc3e72 | 3008 | } |
3009 | else | |
3010 | { | |
3011 | /* Here we must do the comparison on the nominal type | |
3012 | using the args exactly as we received them. */ | |
3013 | type = *restype_ptr; | |
3014 | primop0 = op0; | |
3015 | primop1 = op1; | |
3016 | ||
3017 | if (!real1 && !real2 && integer_zerop (primop1) | |
78a8ed03 | 3018 | && TYPE_UNSIGNED (*restype_ptr)) |
b0fc3e72 | 3019 | { |
3020 | tree value = 0; | |
3021 | switch (code) | |
3022 | { | |
3023 | case GE_EXPR: | |
e6e352cb | 3024 | /* All unsigned values are >= 0, so we warn. However, |
3025 | if OP0 is a constant that is >= 0, the signedness of | |
3026 | the comparison isn't an issue, so suppress the | |
3027 | warning. */ | |
3028 | if (warn_type_limits && !in_system_header | |
84166705 | 3029 | && !(TREE_CODE (primop0) == INTEGER_CST |
3030 | && !TREE_OVERFLOW (convert (c_common_signed_type (type), | |
3031 | primop0)))) | |
e6e352cb | 3032 | warning (OPT_Wtype_limits, |
3033 | "comparison of unsigned expression >= 0 is always true"); | |
3c2239cf | 3034 | value = truthvalue_true_node; |
b0fc3e72 | 3035 | break; |
3036 | ||
3037 | case LT_EXPR: | |
e6e352cb | 3038 | if (warn_type_limits && !in_system_header |
84166705 | 3039 | && !(TREE_CODE (primop0) == INTEGER_CST |
3040 | && !TREE_OVERFLOW (convert (c_common_signed_type (type), | |
3041 | primop0)))) | |
e6e352cb | 3042 | warning (OPT_Wtype_limits, |
3043 | "comparison of unsigned expression < 0 is always false"); | |
3c2239cf | 3044 | value = truthvalue_false_node; |
31f820d2 | 3045 | break; |
3046 | ||
3047 | default: | |
3048 | break; | |
b0fc3e72 | 3049 | } |
3050 | ||
3051 | if (value != 0) | |
3052 | { | |
3053 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
3054 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 3055 | return build2 (COMPOUND_EXPR, TREE_TYPE (value), |
3056 | primop0, value); | |
b0fc3e72 | 3057 | return value; |
3058 | } | |
3059 | } | |
3060 | } | |
3061 | ||
3062 | *op0_ptr = convert (type, primop0); | |
3063 | *op1_ptr = convert (type, primop1); | |
3064 | ||
3c2239cf | 3065 | *restype_ptr = truthvalue_type_node; |
b0fc3e72 | 3066 | |
3067 | return 0; | |
3068 | } | |
3069 | \f | |
1c26100f | 3070 | /* Return a tree for the sum or difference (RESULTCODE says which) |
3071 | of pointer PTROP and integer INTOP. */ | |
3072 | ||
3073 | tree | |
1cae46be | 3074 | pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop) |
1c26100f | 3075 | { |
add6ee5e | 3076 | tree size_exp, ret; |
1c26100f | 3077 | |
1c26100f | 3078 | /* The result is a pointer of the same type that is being added. */ |
1c26100f | 3079 | tree result_type = TREE_TYPE (ptrop); |
3080 | ||
3081 | if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) | |
3082 | { | |
8864917d | 3083 | pedwarn (pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
3084 | "pointer of type %<void *%> used in arithmetic"); | |
1c26100f | 3085 | size_exp = integer_one_node; |
3086 | } | |
3087 | else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) | |
3088 | { | |
8864917d | 3089 | pedwarn (pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
3090 | "pointer to a function used in arithmetic"); | |
1c26100f | 3091 | size_exp = integer_one_node; |
3092 | } | |
3093 | else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE) | |
3094 | { | |
8864917d | 3095 | pedwarn (pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
3096 | "pointer to member function used in arithmetic"); | |
1c26100f | 3097 | size_exp = integer_one_node; |
3098 | } | |
1c26100f | 3099 | else |
3100 | size_exp = size_in_bytes (TREE_TYPE (result_type)); | |
3101 | ||
add6ee5e | 3102 | /* We are manipulating pointer values, so we don't need to warn |
3103 | about relying on undefined signed overflow. We disable the | |
3104 | warning here because we use integer types so fold won't know that | |
3105 | they are really pointers. */ | |
3106 | fold_defer_overflow_warnings (); | |
3107 | ||
1c26100f | 3108 | /* If what we are about to multiply by the size of the elements |
3109 | contains a constant term, apply distributive law | |
3110 | and multiply that constant term separately. | |
3111 | This helps produce common subexpressions. */ | |
1c26100f | 3112 | if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) |
84166705 | 3113 | && !TREE_CONSTANT (intop) |
1c26100f | 3114 | && TREE_CONSTANT (TREE_OPERAND (intop, 1)) |
3115 | && TREE_CONSTANT (size_exp) | |
3116 | /* If the constant comes from pointer subtraction, | |
3117 | skip this optimization--it would cause an error. */ | |
3118 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE | |
3119 | /* If the constant is unsigned, and smaller than the pointer size, | |
3120 | then we must skip this optimization. This is because it could cause | |
3121 | an overflow error if the constant is negative but INTOP is not. */ | |
84166705 | 3122 | && (!TYPE_UNSIGNED (TREE_TYPE (intop)) |
1c26100f | 3123 | || (TYPE_PRECISION (TREE_TYPE (intop)) |
3124 | == TYPE_PRECISION (TREE_TYPE (ptrop))))) | |
3125 | { | |
3126 | enum tree_code subcode = resultcode; | |
3127 | tree int_type = TREE_TYPE (intop); | |
3128 | if (TREE_CODE (intop) == MINUS_EXPR) | |
3129 | subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); | |
3130 | /* Convert both subexpression types to the type of intop, | |
3131 | because weird cases involving pointer arithmetic | |
3132 | can result in a sum or difference with different type args. */ | |
3133 | ptrop = build_binary_op (subcode, ptrop, | |
3134 | convert (int_type, TREE_OPERAND (intop, 1)), 1); | |
3135 | intop = convert (int_type, TREE_OPERAND (intop, 0)); | |
3136 | } | |
3137 | ||
3138 | /* Convert the integer argument to a type the same size as sizetype | |
3139 | so the multiply won't overflow spuriously. */ | |
1c26100f | 3140 | if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) |
78a8ed03 | 3141 | || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype)) |
1cae46be | 3142 | intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype), |
78a8ed03 | 3143 | TYPE_UNSIGNED (sizetype)), intop); |
1c26100f | 3144 | |
3145 | /* Replace the integer argument with a suitable product by the object size. | |
3146 | Do this multiplication as signed, then convert to the appropriate | |
499e523f | 3147 | type for the pointer operation. */ |
3148 | intop = convert (sizetype, | |
3149 | build_binary_op (MULT_EXPR, intop, | |
3150 | convert (TREE_TYPE (intop), size_exp), 1)); | |
0de36bdb | 3151 | |
499e523f | 3152 | /* Create the sum or difference. */ |
0de36bdb | 3153 | if (resultcode == MINUS_EXPR) |
499e523f | 3154 | intop = fold_build1 (NEGATE_EXPR, sizetype, intop); |
1c26100f | 3155 | |
0de36bdb | 3156 | ret = fold_build2 (POINTER_PLUS_EXPR, result_type, ptrop, intop); |
add6ee5e | 3157 | |
3158 | fold_undefer_and_ignore_overflow_warnings (); | |
3159 | ||
3160 | return ret; | |
1c26100f | 3161 | } |
3162 | \f | |
6b68e71a | 3163 | /* Return whether EXPR is a declaration whose address can never be |
3164 | NULL. */ | |
3165 | ||
3166 | bool | |
9f627b1a | 3167 | decl_with_nonnull_addr_p (const_tree expr) |
6b68e71a | 3168 | { |
3169 | return (DECL_P (expr) | |
3170 | && (TREE_CODE (expr) == PARM_DECL | |
3171 | || TREE_CODE (expr) == LABEL_DECL | |
3172 | || !DECL_WEAK (expr))); | |
3173 | } | |
3174 | ||
b0fc3e72 | 3175 | /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, |
45a78cc0 | 3176 | or for an `if' or `while' statement or ?..: exp. It should already |
3177 | have been validated to be of suitable type; otherwise, a bad | |
3178 | diagnostic may result. | |
b0fc3e72 | 3179 | |
3180 | This preparation consists of taking the ordinary | |
3181 | representation of an expression expr and producing a valid tree | |
3182 | boolean expression describing whether expr is nonzero. We could | |
3c2239cf | 3183 | simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1), |
b0fc3e72 | 3184 | but we optimize comparisons, &&, ||, and !. |
3185 | ||
3c2239cf | 3186 | The resulting type should always be `truthvalue_type_node'. */ |
b0fc3e72 | 3187 | |
3188 | tree | |
1cae46be | 3189 | c_common_truthvalue_conversion (tree expr) |
b0fc3e72 | 3190 | { |
b0fc3e72 | 3191 | switch (TREE_CODE (expr)) |
3192 | { | |
318a728f | 3193 | case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR: |
e1232ce2 | 3194 | case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: |
3195 | case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR: | |
3196 | case ORDERED_EXPR: case UNORDERED_EXPR: | |
ce04dcdc | 3197 | if (TREE_TYPE (expr) == truthvalue_type_node) |
3198 | return expr; | |
3199 | return build2 (TREE_CODE (expr), truthvalue_type_node, | |
3200 | TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); | |
3201 | ||
b0fc3e72 | 3202 | case TRUTH_ANDIF_EXPR: |
3203 | case TRUTH_ORIF_EXPR: | |
3204 | case TRUTH_AND_EXPR: | |
3205 | case TRUTH_OR_EXPR: | |
31f6e93c | 3206 | case TRUTH_XOR_EXPR: |
ce04dcdc | 3207 | if (TREE_TYPE (expr) == truthvalue_type_node) |
3208 | return expr; | |
3209 | return build2 (TREE_CODE (expr), truthvalue_type_node, | |
45a78cc0 | 3210 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)), |
3211 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 1))); | |
3e851b85 | 3212 | |
f6e28f72 | 3213 | case TRUTH_NOT_EXPR: |
ce04dcdc | 3214 | if (TREE_TYPE (expr) == truthvalue_type_node) |
3215 | return expr; | |
3216 | return build1 (TREE_CODE (expr), truthvalue_type_node, | |
45a78cc0 | 3217 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 0))); |
f6e28f72 | 3218 | |
b0fc3e72 | 3219 | case ERROR_MARK: |
3220 | return expr; | |
3221 | ||
3222 | case INTEGER_CST: | |
eddad94a | 3223 | return integer_zerop (expr) ? truthvalue_false_node |
3224 | : truthvalue_true_node; | |
b0fc3e72 | 3225 | |
3226 | case REAL_CST: | |
5000e21c | 3227 | return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0) |
3228 | ? truthvalue_true_node | |
3229 | : truthvalue_false_node; | |
b0fc3e72 | 3230 | |
9421ebb9 | 3231 | case FIXED_CST: |
3232 | return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr), | |
3233 | &FCONST0 (TYPE_MODE (TREE_TYPE (expr)))) | |
3234 | ? truthvalue_true_node | |
3235 | : truthvalue_false_node; | |
3236 | ||
ce04dcdc | 3237 | case FUNCTION_DECL: |
3238 | expr = build_unary_op (ADDR_EXPR, expr, 0); | |
3239 | /* Fall through. */ | |
3240 | ||
b0fc3e72 | 3241 | case ADDR_EXPR: |
ce871053 | 3242 | { |
ee7d4d6a | 3243 | tree inner = TREE_OPERAND (expr, 0); |
6b68e71a | 3244 | if (decl_with_nonnull_addr_p (inner)) |
ce871053 | 3245 | { |
6b68e71a | 3246 | /* Common Ada/Pascal programmer's mistake. */ |
ede5a481 | 3247 | warning (OPT_Waddress, |
6b68e71a | 3248 | "the address of %qD will always evaluate as %<true%>", |
ee7d4d6a | 3249 | inner); |
ce871053 | 3250 | return truthvalue_true_node; |
3251 | } | |
65b5e6a6 | 3252 | |
6b68e71a | 3253 | /* If we still have a decl, it is possible for its address to |
3254 | be NULL, so we cannot optimize. */ | |
3255 | if (DECL_P (inner)) | |
3256 | { | |
3257 | gcc_assert (DECL_WEAK (inner)); | |
3258 | break; | |
3259 | } | |
ce871053 | 3260 | |
ee7d4d6a | 3261 | if (TREE_SIDE_EFFECTS (inner)) |
14ae0310 | 3262 | return build2 (COMPOUND_EXPR, truthvalue_type_node, |
ee7d4d6a | 3263 | inner, truthvalue_true_node); |
ce871053 | 3264 | else |
3265 | return truthvalue_true_node; | |
3266 | } | |
b0fc3e72 | 3267 | |
2203bd5c | 3268 | case COMPLEX_EXPR: |
2ba726d2 | 3269 | return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) |
95809de4 | 3270 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
45a78cc0 | 3271 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)), |
3272 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)), | |
2203bd5c | 3273 | 0); |
3274 | ||
b0fc3e72 | 3275 | case NEGATE_EXPR: |
3276 | case ABS_EXPR: | |
3277 | case FLOAT_EXPR: | |
d10cfa8d | 3278 | /* These don't change whether an object is nonzero or zero. */ |
45a78cc0 | 3279 | return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)); |
b0fc3e72 | 3280 | |
3281 | case LROTATE_EXPR: | |
3282 | case RROTATE_EXPR: | |
d10cfa8d | 3283 | /* These don't change whether an object is zero or nonzero, but |
b0fc3e72 | 3284 | we can't ignore them if their second arg has side-effects. */ |
3285 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) | |
14ae0310 | 3286 | return build2 (COMPOUND_EXPR, truthvalue_type_node, |
3287 | TREE_OPERAND (expr, 1), | |
45a78cc0 | 3288 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 0))); |
b0fc3e72 | 3289 | else |
45a78cc0 | 3290 | return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)); |
73be5127 | 3291 | |
b0fc3e72 | 3292 | case COND_EXPR: |
3293 | /* Distribute the conversion into the arms of a COND_EXPR. */ | |
49d00087 | 3294 | return fold_build3 (COND_EXPR, truthvalue_type_node, |
14ae0310 | 3295 | TREE_OPERAND (expr, 0), |
45a78cc0 | 3296 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)), |
49d00087 | 3297 | c_common_truthvalue_conversion (TREE_OPERAND (expr, 2))); |
b0fc3e72 | 3298 | |
72dd6141 | 3299 | CASE_CONVERT: |
b0fc3e72 | 3300 | /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, |
3301 | since that affects how `default_conversion' will behave. */ | |
3302 | if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE | |
3303 | || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE) | |
3304 | break; | |
b0fc3e72 | 3305 | /* If this is widening the argument, we can ignore it. */ |
3306 | if (TYPE_PRECISION (TREE_TYPE (expr)) | |
3307 | >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0)))) | |
45a78cc0 | 3308 | return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)); |
b0fc3e72 | 3309 | break; |
3310 | ||
16837b18 | 3311 | case MODIFY_EXPR: |
60a0513e | 3312 | if (!TREE_NO_WARNING (expr) |
3313 | && warn_parentheses) | |
3314 | { | |
3315 | warning (OPT_Wparentheses, | |
3316 | "suggest parentheses around assignment used as truth value"); | |
3317 | TREE_NO_WARNING (expr) = 1; | |
3318 | } | |
16837b18 | 3319 | break; |
73be5127 | 3320 | |
31f820d2 | 3321 | default: |
3322 | break; | |
b0fc3e72 | 3323 | } |
3324 | ||
2ba726d2 | 3325 | if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) |
a0748b7d | 3326 | { |
aff9e656 | 3327 | tree t = save_expr (expr); |
a0748b7d | 3328 | return (build_binary_op |
3329 | ((TREE_SIDE_EFFECTS (expr) | |
3330 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), | |
45a78cc0 | 3331 | c_common_truthvalue_conversion (build_unary_op (REALPART_EXPR, t, 0)), |
3332 | c_common_truthvalue_conversion (build_unary_op (IMAGPART_EXPR, t, 0)), | |
a0748b7d | 3333 | 0)); |
3334 | } | |
2ba726d2 | 3335 | |
9421ebb9 | 3336 | if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE) |
3337 | { | |
3338 | tree fixed_zero_node = build_fixed (TREE_TYPE (expr), | |
3339 | FCONST0 (TYPE_MODE | |
3340 | (TREE_TYPE (expr)))); | |
3341 | return build_binary_op (NE_EXPR, expr, fixed_zero_node, 1); | |
3342 | } | |
3343 | ||
b0fc3e72 | 3344 | return build_binary_op (NE_EXPR, expr, integer_zero_node, 1); |
3345 | } | |
3346 | \f | |
3237155d | 3347 | static void def_builtin_1 (enum built_in_function fncode, |
3348 | const char *name, | |
3349 | enum built_in_class fnclass, | |
3350 | tree fntype, tree libtype, | |
3351 | bool both_p, bool fallback_p, bool nonansi_p, | |
3352 | tree fnattrs, bool implicit_p); | |
0d4238dc | 3353 | |
a5b1863e | 3354 | |
3355 | /* Apply the TYPE_QUALS to the new DECL. */ | |
3356 | ||
3357 | void | |
1cae46be | 3358 | c_apply_type_quals_to_decl (int type_quals, tree decl) |
a5b1863e | 3359 | { |
adfb367f | 3360 | tree type = TREE_TYPE (decl); |
b27ac6b5 | 3361 | |
e4eabbe4 | 3362 | if (type == error_mark_node) |
3363 | return; | |
adfb367f | 3364 | |
3365 | if (((type_quals & TYPE_QUAL_CONST) | |
3366 | || (type && TREE_CODE (type) == REFERENCE_TYPE)) | |
3367 | /* An object declared 'const' is only readonly after it is | |
3368 | initialized. We don't have any way of expressing this currently, | |
3369 | so we need to be conservative and unset TREE_READONLY for types | |
3370 | with constructors. Otherwise aliasing code will ignore stores in | |
3371 | an inline constructor. */ | |
3372 | && !(type && TYPE_NEEDS_CONSTRUCTING (type))) | |
a5b1863e | 3373 | TREE_READONLY (decl) = 1; |
3374 | if (type_quals & TYPE_QUAL_VOLATILE) | |
3375 | { | |
3376 | TREE_SIDE_EFFECTS (decl) = 1; | |
3377 | TREE_THIS_VOLATILE (decl) = 1; | |
3378 | } | |
d91a20bc | 3379 | if (type_quals & TYPE_QUAL_RESTRICT) |
a5b1863e | 3380 | { |
adfb367f | 3381 | while (type && TREE_CODE (type) == ARRAY_TYPE) |
3382 | /* Allow 'restrict' on arrays of pointers. | |
3383 | FIXME currently we just ignore it. */ | |
3384 | type = TREE_TYPE (type); | |
3385 | if (!type | |
3386 | || !POINTER_TYPE_P (type) | |
3387 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))) | |
b0b1af64 | 3388 | error ("invalid use of %<restrict%>"); |
adfb367f | 3389 | else if (flag_strict_aliasing && type == TREE_TYPE (decl)) |
ed83aafb | 3390 | /* Indicate we need to make a unique alias set for this pointer. |
3391 | We can't do it here because it might be pointing to an | |
3392 | incomplete type. */ | |
3393 | DECL_POINTER_ALIAS_SET (decl) = -2; | |
a5b1863e | 3394 | } |
3395 | } | |
3396 | ||
4ee9c684 | 3397 | /* Hash function for the problem of multiple type definitions in |
3398 | different files. This must hash all types that will compare | |
3399 | equal via comptypes to the same value. In practice it hashes | |
0bed3869 | 3400 | on some of the simple stuff and leaves the details to comptypes. */ |
4ee9c684 | 3401 | |
3402 | static hashval_t | |
3403 | c_type_hash (const void *p) | |
3404 | { | |
3405 | int i = 0; | |
3406 | int shift, size; | |
aae87fc3 | 3407 | const_tree const t = (const_tree) p; |
4ee9c684 | 3408 | tree t2; |
3409 | switch (TREE_CODE (t)) | |
3410 | { | |
fbf0afd1 | 3411 | /* For pointers, hash on pointee type plus some swizzling. */ |
2363ef00 | 3412 | case POINTER_TYPE: |
3413 | return c_type_hash (TREE_TYPE (t)) ^ 0x3003003; | |
3414 | /* Hash on number of elements and total size. */ | |
3415 | case ENUMERAL_TYPE: | |
3416 | shift = 3; | |
3417 | t2 = TYPE_VALUES (t); | |
3418 | break; | |
3419 | case RECORD_TYPE: | |
3420 | shift = 0; | |
3421 | t2 = TYPE_FIELDS (t); | |
3422 | break; | |
3423 | case QUAL_UNION_TYPE: | |
3424 | shift = 1; | |
3425 | t2 = TYPE_FIELDS (t); | |
3426 | break; | |
3427 | case UNION_TYPE: | |
3428 | shift = 2; | |
3429 | t2 = TYPE_FIELDS (t); | |
3430 | break; | |
3431 | default: | |
231bd014 | 3432 | gcc_unreachable (); |
4ee9c684 | 3433 | } |
3434 | for (; t2; t2 = TREE_CHAIN (t2)) | |
3435 | i++; | |
78c2e180 | 3436 | /* We might have a VLA here. */ |
3437 | if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST) | |
3438 | size = 0; | |
3439 | else | |
3440 | size = TREE_INT_CST_LOW (TYPE_SIZE (t)); | |
4ee9c684 | 3441 | return ((size << 24) | (i << shift)); |
3442 | } | |
3443 | ||
1ecd4018 | 3444 | static GTY((param_is (union tree_node))) htab_t type_hash_table; |
3445 | ||
b5ba9f3a | 3446 | /* Return the typed-based alias set for T, which may be an expression |
f7c44134 | 3447 | or a type. Return -1 if we don't do anything special. */ |
b5ba9f3a | 3448 | |
32c2fdea | 3449 | alias_set_type |
1cae46be | 3450 | c_common_get_alias_set (tree t) |
b5ba9f3a | 3451 | { |
be4f2de7 | 3452 | tree u; |
4ee9c684 | 3453 | PTR *slot; |
1cae46be | 3454 | |
be4f2de7 | 3455 | /* Permit type-punning when accessing a union, provided the access |
3456 | is directly through the union. For example, this code does not | |
3457 | permit taking the address of a union member and then storing | |
3458 | through it. Even the type-punning allowed here is a GCC | |
3459 | extension, albeit a common and useful one; the C standard says | |
3460 | that such accesses have implementation-defined behavior. */ | |
3461 | for (u = t; | |
3462 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
3463 | u = TREE_OPERAND (u, 0)) | |
3464 | if (TREE_CODE (u) == COMPONENT_REF | |
3465 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
3466 | return 0; | |
1e2513d9 | 3467 | |
9fcc3e54 | 3468 | /* That's all the expressions we handle specially. */ |
84166705 | 3469 | if (!TYPE_P (t)) |
9fcc3e54 | 3470 | return -1; |
3471 | ||
d716ce75 | 3472 | /* The C standard guarantees that any object may be accessed via an |
9fcc3e54 | 3473 | lvalue that has character type. */ |
3474 | if (t == char_type_node | |
3475 | || t == signed_char_type_node | |
3476 | || t == unsigned_char_type_node) | |
f7c44134 | 3477 | return 0; |
a5b1863e | 3478 | |
1607663f | 3479 | /* The C standard specifically allows aliasing between signed and |
3480 | unsigned variants of the same type. We treat the signed | |
3481 | variant as canonical. */ | |
78a8ed03 | 3482 | if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t)) |
a8868e19 | 3483 | { |
4070745f | 3484 | tree t1 = c_common_signed_type (t); |
1607663f | 3485 | |
a8868e19 | 3486 | /* t1 == t can happen for boolean nodes which are always unsigned. */ |
3487 | if (t1 != t) | |
3488 | return get_alias_set (t1); | |
3489 | } | |
f7c44134 | 3490 | else if (POINTER_TYPE_P (t)) |
87d2d17e | 3491 | { |
f7c44134 | 3492 | tree t1; |
87d2d17e | 3493 | |
3494 | /* Unfortunately, there is no canonical form of a pointer type. | |
3495 | In particular, if we have `typedef int I', then `int *', and | |
3496 | `I *' are different types. So, we have to pick a canonical | |
3497 | representative. We do this below. | |
44e9fa65 | 3498 | |
40bdc593 | 3499 | Technically, this approach is actually more conservative that |
3500 | it needs to be. In particular, `const int *' and `int *' | |
734c98be | 3501 | should be in different alias sets, according to the C and C++ |
40bdc593 | 3502 | standard, since their types are not the same, and so, |
3503 | technically, an `int **' and `const int **' cannot point at | |
3504 | the same thing. | |
3505 | ||
4ee9c684 | 3506 | But, the standard is wrong. In particular, this code is |
40bdc593 | 3507 | legal C++: |
3508 | ||
a0c938f0 | 3509 | int *ip; |
3510 | int **ipp = &ip; | |
3511 | const int* const* cipp = ipp; | |
40bdc593 | 3512 | |
4ee9c684 | 3513 | And, it doesn't make sense for that to be legal unless you |
40bdc593 | 3514 | can dereference IPP and CIPP. So, we ignore cv-qualifiers on |
3515 | the pointed-to types. This issue has been reported to the | |
3516 | C++ committee. */ | |
211f3116 | 3517 | t1 = build_type_no_quals (t); |
f7c44134 | 3518 | if (t1 != t) |
3519 | return get_alias_set (t1); | |
87d2d17e | 3520 | } |
1e2513d9 | 3521 | |
4ee9c684 | 3522 | /* Handle the case of multiple type nodes referring to "the same" type, |
3523 | which occurs with IMA. These share an alias set. FIXME: Currently only | |
3524 | C90 is handled. (In C99 type compatibility is not transitive, which | |
3525 | complicates things mightily. The alias set splay trees can theoretically | |
3526 | represent this, but insertion is tricky when you consider all the | |
3527 | different orders things might arrive in.) */ | |
3528 | ||
3529 | if (c_language != clk_c || flag_isoc99) | |
3530 | return -1; | |
3531 | ||
0bed3869 | 3532 | /* Save time if there's only one input file. */ |
e08bd2f4 | 3533 | if (num_in_fnames == 1) |
4ee9c684 | 3534 | return -1; |
3535 | ||
3536 | /* Pointers need special handling if they point to any type that | |
3537 | needs special handling (below). */ | |
3538 | if (TREE_CODE (t) == POINTER_TYPE) | |
3539 | { | |
3540 | tree t2; | |
3541 | /* Find bottom type under any nested POINTERs. */ | |
b27ac6b5 | 3542 | for (t2 = TREE_TYPE (t); |
af592f67 | 3543 | TREE_CODE (t2) == POINTER_TYPE; |
3544 | t2 = TREE_TYPE (t2)) | |
3545 | ; | |
b27ac6b5 | 3546 | if (TREE_CODE (t2) != RECORD_TYPE |
af592f67 | 3547 | && TREE_CODE (t2) != ENUMERAL_TYPE |
3548 | && TREE_CODE (t2) != QUAL_UNION_TYPE | |
3549 | && TREE_CODE (t2) != UNION_TYPE) | |
3550 | return -1; | |
4ee9c684 | 3551 | if (TYPE_SIZE (t2) == 0) |
af592f67 | 3552 | return -1; |
4ee9c684 | 3553 | } |
3554 | /* These are the only cases that need special handling. */ | |
b27ac6b5 | 3555 | if (TREE_CODE (t) != RECORD_TYPE |
4ee9c684 | 3556 | && TREE_CODE (t) != ENUMERAL_TYPE |
3557 | && TREE_CODE (t) != QUAL_UNION_TYPE | |
3558 | && TREE_CODE (t) != UNION_TYPE | |
3559 | && TREE_CODE (t) != POINTER_TYPE) | |
3560 | return -1; | |
3561 | /* Undefined? */ | |
3562 | if (TYPE_SIZE (t) == 0) | |
3563 | return -1; | |
3564 | ||
b27ac6b5 | 3565 | /* Look up t in hash table. Only one of the compatible types within each |
4ee9c684 | 3566 | alias set is recorded in the table. */ |
3567 | if (!type_hash_table) | |
1ecd4018 | 3568 | type_hash_table = htab_create_ggc (1021, c_type_hash, |
4ee9c684 | 3569 | (htab_eq) lang_hooks.types_compatible_p, |
3570 | NULL); | |
3571 | slot = htab_find_slot (type_hash_table, t, INSERT); | |
3572 | if (*slot != NULL) | |
ad16cb2c | 3573 | { |
3574 | TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot); | |
3575 | return TYPE_ALIAS_SET ((tree)*slot); | |
3576 | } | |
4ee9c684 | 3577 | else |
3578 | /* Our caller will assign and record (in t) a new alias set; all we need | |
3579 | to do is remember t in the hash table. */ | |
3580 | *slot = t; | |
3581 | ||
f7c44134 | 3582 | return -1; |
b5ba9f3a | 3583 | } |
902b4e01 | 3584 | \f |
d4c4d95c | 3585 | /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where the |
d3a4d008 | 3586 | second parameter indicates which OPERATOR is being applied. The COMPLAIN |
3587 | flag controls whether we should diagnose possibly ill-formed | |
3588 | constructs or not. */ | |
908c697e | 3589 | |
902b4e01 | 3590 | tree |
908c697e | 3591 | c_sizeof_or_alignof_type (tree type, bool is_sizeof, int complain) |
902b4e01 | 3592 | { |
d4c4d95c | 3593 | const char *op_name; |
3594 | tree value = NULL; | |
3595 | enum tree_code type_code = TREE_CODE (type); | |
1cae46be | 3596 | |
908c697e | 3597 | op_name = is_sizeof ? "sizeof" : "__alignof__"; |
1cae46be | 3598 | |
d4c4d95c | 3599 | if (type_code == FUNCTION_TYPE) |
902b4e01 | 3600 | { |
908c697e | 3601 | if (is_sizeof) |
d4c4d95c | 3602 | { |
d3a4d008 | 3603 | if (complain && (pedantic || warn_pointer_arith)) |
8864917d | 3604 | pedwarn (pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
3605 | "invalid application of %<sizeof%> to a function type"); | |
ebd21de4 | 3606 | else if (!complain) |
3607 | return error_mark_node; | |
d4c4d95c | 3608 | value = size_one_node; |
3609 | } | |
3610 | else | |
3611 | value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); | |
3612 | } | |
3613 | else if (type_code == VOID_TYPE || type_code == ERROR_MARK) | |
3614 | { | |
1cae46be | 3615 | if (type_code == VOID_TYPE |
d3a4d008 | 3616 | && complain && (pedantic || warn_pointer_arith)) |
8864917d | 3617 | pedwarn (pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
3618 | "invalid application of %qs to a void type", op_name); | |
ebd21de4 | 3619 | else if (!complain) |
3620 | return error_mark_node; | |
d4c4d95c | 3621 | value = size_one_node; |
902b4e01 | 3622 | } |
902b4e01 | 3623 | else if (!COMPLETE_TYPE_P (type)) |
3624 | { | |
d3a4d008 | 3625 | if (complain) |
b0b1af64 | 3626 | error ("invalid application of %qs to incomplete type %qT ", |
3ad0e8cc | 3627 | op_name, type); |
d4c4d95c | 3628 | value = size_zero_node; |
902b4e01 | 3629 | } |
3630 | else | |
d4c4d95c | 3631 | { |
908c697e | 3632 | if (is_sizeof) |
d4c4d95c | 3633 | /* Convert in case a char is more than one unit. */ |
3634 | value = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), | |
3635 | size_int (TYPE_PRECISION (char_type_node) | |
3636 | / BITS_PER_UNIT)); | |
3637 | else | |
d37625c0 | 3638 | value = size_int (TYPE_ALIGN_UNIT (type)); |
d4c4d95c | 3639 | } |
902b4e01 | 3640 | |
d4c4d95c | 3641 | /* VALUE will have an integer type with TYPE_IS_SIZETYPE set. |
3642 | TYPE_IS_SIZETYPE means that certain things (like overflow) will | |
3643 | never happen. However, this node should really have type | |
3644 | `size_t', which is just a typedef for an ordinary integer type. */ | |
143d62d7 | 3645 | value = fold_convert (size_type_node, value); |
1d8fc210 | 3646 | gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value))); |
1cae46be | 3647 | |
d4c4d95c | 3648 | return value; |
902b4e01 | 3649 | } |
3650 | ||
3651 | /* Implement the __alignof keyword: Return the minimum required | |
097b5c8b | 3652 | alignment of EXPR, measured in bytes. For VAR_DECLs, |
3653 | FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set | |
3654 | from an "aligned" __attribute__ specification). */ | |
72040e7e | 3655 | |
902b4e01 | 3656 | tree |
1cae46be | 3657 | c_alignof_expr (tree expr) |
902b4e01 | 3658 | { |
3659 | tree t; | |
3660 | ||
097b5c8b | 3661 | if (VAR_OR_FUNCTION_DECL_P (expr)) |
d37625c0 | 3662 | t = size_int (DECL_ALIGN_UNIT (expr)); |
1cae46be | 3663 | |
902b4e01 | 3664 | else if (TREE_CODE (expr) == COMPONENT_REF |
3665 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
3666 | { | |
b0b1af64 | 3667 | error ("%<__alignof%> applied to a bit-field"); |
902b4e01 | 3668 | t = size_one_node; |
3669 | } | |
3670 | else if (TREE_CODE (expr) == COMPONENT_REF | |
7cc7e163 | 3671 | && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) |
d37625c0 | 3672 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1))); |
1cae46be | 3673 | |
902b4e01 | 3674 | else if (TREE_CODE (expr) == INDIRECT_REF) |
3675 | { | |
3676 | tree t = TREE_OPERAND (expr, 0); | |
3677 | tree best = t; | |
3678 | int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
1cae46be | 3679 | |
72dd6141 | 3680 | while (CONVERT_EXPR_P (t) |
7cc7e163 | 3681 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) |
902b4e01 | 3682 | { |
3683 | int thisalign; | |
3684 | ||
3685 | t = TREE_OPERAND (t, 0); | |
3686 | thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
3687 | if (thisalign > bestalign) | |
3688 | best = t, bestalign = thisalign; | |
3689 | } | |
3690 | return c_alignof (TREE_TYPE (TREE_TYPE (best))); | |
3691 | } | |
3692 | else | |
3693 | return c_alignof (TREE_TYPE (expr)); | |
3694 | ||
143d62d7 | 3695 | return fold_convert (size_type_node, t); |
902b4e01 | 3696 | } |
3697 | \f | |
8fe4a266 | 3698 | /* Handle C and C++ default attributes. */ |
3699 | ||
3700 | enum built_in_attribute | |
3701 | { | |
3702 | #define DEF_ATTR_NULL_TREE(ENUM) ENUM, | |
3703 | #define DEF_ATTR_INT(ENUM, VALUE) ENUM, | |
3704 | #define DEF_ATTR_IDENT(ENUM, STRING) ENUM, | |
3705 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM, | |
8fe4a266 | 3706 | #include "builtin-attrs.def" |
3707 | #undef DEF_ATTR_NULL_TREE | |
3708 | #undef DEF_ATTR_INT | |
3709 | #undef DEF_ATTR_IDENT | |
3710 | #undef DEF_ATTR_TREE_LIST | |
8fe4a266 | 3711 | ATTR_LAST |
3712 | }; | |
3713 | ||
3714 | static GTY(()) tree built_in_attributes[(int) ATTR_LAST]; | |
3715 | ||
1cae46be | 3716 | static void c_init_attributes (void); |
8fe4a266 | 3717 | |
27213ba3 | 3718 | enum c_builtin_type |
72040e7e | 3719 | { |
d2d4bdde | 3720 | #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME, |
3721 | #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME, | |
3722 | #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME, | |
3723 | #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME, | |
3724 | #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, | |
3725 | #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, | |
0a39fd54 | 3726 | #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME, |
27213ba3 | 3727 | #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME, |
3728 | #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME, | |
d2d4bdde | 3729 | #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME, |
3730 | #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME, | |
3731 | #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME, | |
03901330 | 3732 | #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, |
0a39fd54 | 3733 | #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, |
3734 | #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \ | |
3735 | NAME, | |
d2d4bdde | 3736 | #define DEF_POINTER_TYPE(NAME, TYPE) NAME, |
3737 | #include "builtin-types.def" | |
3738 | #undef DEF_PRIMITIVE_TYPE | |
3739 | #undef DEF_FUNCTION_TYPE_0 | |
3740 | #undef DEF_FUNCTION_TYPE_1 | |
3741 | #undef DEF_FUNCTION_TYPE_2 | |
3742 | #undef DEF_FUNCTION_TYPE_3 | |
3743 | #undef DEF_FUNCTION_TYPE_4 | |
0a39fd54 | 3744 | #undef DEF_FUNCTION_TYPE_5 |
3745 | #undef DEF_FUNCTION_TYPE_6 | |
27213ba3 | 3746 | #undef DEF_FUNCTION_TYPE_7 |
d2d4bdde | 3747 | #undef DEF_FUNCTION_TYPE_VAR_0 |
3748 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
3749 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
03901330 | 3750 | #undef DEF_FUNCTION_TYPE_VAR_3 |
0a39fd54 | 3751 | #undef DEF_FUNCTION_TYPE_VAR_4 |
3752 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
d2d4bdde | 3753 | #undef DEF_POINTER_TYPE |
27213ba3 | 3754 | BT_LAST |
3755 | }; | |
3756 | ||
3757 | typedef enum c_builtin_type builtin_type; | |
d2d4bdde | 3758 | |
27213ba3 | 3759 | /* A temporary array for c_common_nodes_and_builtins. Used in |
3760 | communication with def_fn_type. */ | |
3761 | static tree builtin_types[(int) BT_LAST + 1]; | |
d2d4bdde | 3762 | |
27213ba3 | 3763 | /* A helper function for c_common_nodes_and_builtins. Build function type |
3764 | for DEF with return type RET and N arguments. If VAR is true, then the | |
3765 | function should be variadic after those N arguments. | |
3766 | ||
3767 | Takes special care not to ICE if any of the types involved are | |
3768 | error_mark_node, which indicates that said type is not in fact available | |
3769 | (see builtin_type_for_size). In which case the function type as a whole | |
3770 | should be error_mark_node. */ | |
3771 | ||
3772 | static void | |
3773 | def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...) | |
3774 | { | |
3775 | tree args = NULL, t; | |
3776 | va_list list; | |
3777 | int i; | |
3778 | ||
3779 | va_start (list, n); | |
3780 | for (i = 0; i < n; ++i) | |
3781 | { | |
7d339f93 | 3782 | builtin_type a = (builtin_type) va_arg (list, int); |
27213ba3 | 3783 | t = builtin_types[a]; |
3784 | if (t == error_mark_node) | |
3785 | goto egress; | |
3786 | args = tree_cons (NULL_TREE, t, args); | |
3787 | } | |
3788 | va_end (list); | |
3789 | ||
3790 | args = nreverse (args); | |
3791 | if (!var) | |
3792 | args = chainon (args, void_list_node); | |
3793 | ||
3794 | t = builtin_types[ret]; | |
3795 | if (t == error_mark_node) | |
3796 | goto egress; | |
3797 | t = build_function_type (t, args); | |
3798 | ||
3799 | egress: | |
3800 | builtin_types[def] = t; | |
3801 | } | |
3802 | ||
dce22712 | 3803 | /* Build builtin functions common to both C and C++ language |
3804 | frontends. */ | |
3805 | ||
3806 | static void | |
3807 | c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node) | |
3808 | { | |
3809 | #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ | |
3810 | builtin_types[ENUM] = VALUE; | |
3811 | #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ | |
3812 | def_fn_type (ENUM, RETURN, 0, 0); | |
3813 | #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ | |
3814 | def_fn_type (ENUM, RETURN, 0, 1, ARG1); | |
3815 | #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ | |
3816 | def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2); | |
3817 | #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
3818 | def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3); | |
3819 | #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
3820 | def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4); | |
3821 | #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
3822 | def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
3823 | #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
3824 | ARG6) \ | |
3825 | def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); | |
3826 | #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
3827 | ARG6, ARG7) \ | |
3828 | def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); | |
3829 | #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ | |
3830 | def_fn_type (ENUM, RETURN, 1, 0); | |
3831 | #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ | |
3832 | def_fn_type (ENUM, RETURN, 1, 1, ARG1); | |
3833 | #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ | |
3834 | def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2); | |
3835 | #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
3836 | def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3); | |
3837 | #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
3838 | def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4); | |
3839 | #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
3840 | def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
3841 | #define DEF_POINTER_TYPE(ENUM, TYPE) \ | |
3842 | builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]); | |
3843 | ||
3844 | #include "builtin-types.def" | |
3845 | ||
3846 | #undef DEF_PRIMITIVE_TYPE | |
3847 | #undef DEF_FUNCTION_TYPE_1 | |
3848 | #undef DEF_FUNCTION_TYPE_2 | |
3849 | #undef DEF_FUNCTION_TYPE_3 | |
3850 | #undef DEF_FUNCTION_TYPE_4 | |
3851 | #undef DEF_FUNCTION_TYPE_5 | |
3852 | #undef DEF_FUNCTION_TYPE_6 | |
3853 | #undef DEF_FUNCTION_TYPE_VAR_0 | |
3854 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
3855 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
3856 | #undef DEF_FUNCTION_TYPE_VAR_3 | |
3857 | #undef DEF_FUNCTION_TYPE_VAR_4 | |
3858 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
3859 | #undef DEF_POINTER_TYPE | |
3860 | builtin_types[(int) BT_LAST] = NULL_TREE; | |
3861 | ||
3862 | c_init_attributes (); | |
3863 | ||
3864 | #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ | |
3865 | NONANSI_P, ATTRS, IMPLICIT, COND) \ | |
3866 | if (NAME && COND) \ | |
3867 | def_builtin_1 (ENUM, NAME, CLASS, \ | |
3868 | builtin_types[(int) TYPE], \ | |
3869 | builtin_types[(int) LIBTYPE], \ | |
3870 | BOTH_P, FALLBACK_P, NONANSI_P, \ | |
3871 | built_in_attributes[(int) ATTRS], IMPLICIT); | |
3872 | #include "builtins.def" | |
3873 | #undef DEF_BUILTIN | |
3874 | ||
87eb1c28 | 3875 | targetm.init_builtins (); |
3876 | ||
dce22712 | 3877 | build_common_builtin_nodes (); |
3878 | ||
dce22712 | 3879 | if (flag_mudflap) |
3880 | mudflap_init (); | |
3881 | } | |
3882 | ||
27213ba3 | 3883 | /* Build tree nodes and builtin functions common to both C and C++ language |
3884 | frontends. */ | |
3885 | ||
3886 | void | |
3887 | c_common_nodes_and_builtins (void) | |
3888 | { | |
924bbf02 | 3889 | int char16_type_size; |
3890 | int char32_type_size; | |
174fcc61 | 3891 | int wchar_type_size; |
3892 | tree array_domain_type; | |
2d47cc32 | 3893 | tree va_list_ref_type_node; |
8a15c04a | 3894 | tree va_list_arg_type_node; |
a66c9326 | 3895 | |
174fcc61 | 3896 | /* Define `int' and `char' first so that dbx will output them first. */ |
d946ea19 | 3897 | record_builtin_type (RID_INT, NULL, integer_type_node); |
174fcc61 | 3898 | record_builtin_type (RID_CHAR, "char", char_type_node); |
3899 | ||
3900 | /* `signed' is the same as `int'. FIXME: the declarations of "signed", | |
3901 | "unsigned long", "long long unsigned" and "unsigned short" were in C++ | |
3902 | but not C. Are the conditionals here needed? */ | |
c0f19401 | 3903 | if (c_dialect_cxx ()) |
d946ea19 | 3904 | record_builtin_type (RID_SIGNED, NULL, integer_type_node); |
174fcc61 | 3905 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); |
3906 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); | |
3907 | record_builtin_type (RID_MAX, "long unsigned int", | |
3908 | long_unsigned_type_node); | |
c0f19401 | 3909 | if (c_dialect_cxx ()) |
174fcc61 | 3910 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); |
3911 | record_builtin_type (RID_MAX, "long long int", | |
3912 | long_long_integer_type_node); | |
3913 | record_builtin_type (RID_MAX, "long long unsigned int", | |
3914 | long_long_unsigned_type_node); | |
c0f19401 | 3915 | if (c_dialect_cxx ()) |
174fcc61 | 3916 | record_builtin_type (RID_MAX, "long long unsigned", |
3917 | long_long_unsigned_type_node); | |
3918 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); | |
3919 | record_builtin_type (RID_MAX, "short unsigned int", | |
3920 | short_unsigned_type_node); | |
c0f19401 | 3921 | if (c_dialect_cxx ()) |
174fcc61 | 3922 | record_builtin_type (RID_MAX, "unsigned short", |
3923 | short_unsigned_type_node); | |
3924 | ||
3925 | /* Define both `signed char' and `unsigned char'. */ | |
3926 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); | |
3927 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); | |
3928 | ||
771d21fa | 3929 | /* These are types that c_common_type_for_size and |
3930 | c_common_type_for_mode use. */ | |
dc24ddbd | 3931 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, |
3932 | intQI_type_node)); | |
3933 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, | |
3934 | intHI_type_node)); | |
3935 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, | |
3936 | intSI_type_node)); | |
3937 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, | |
3938 | intDI_type_node)); | |
174fcc61 | 3939 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 3940 | if (targetm.scalar_mode_supported_p (TImode)) |
3941 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, | |
3942 | get_identifier ("__int128_t"), | |
3943 | intTI_type_node)); | |
174fcc61 | 3944 | #endif |
dc24ddbd | 3945 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, |
3946 | unsigned_intQI_type_node)); | |
3947 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, | |
3948 | unsigned_intHI_type_node)); | |
3949 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, | |
3950 | unsigned_intSI_type_node)); | |
3951 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, | |
3952 | unsigned_intDI_type_node)); | |
174fcc61 | 3953 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 3954 | if (targetm.scalar_mode_supported_p (TImode)) |
3955 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, | |
3956 | get_identifier ("__uint128_t"), | |
3957 | unsigned_intTI_type_node)); | |
174fcc61 | 3958 | #endif |
3959 | ||
3960 | /* Create the widest literal types. */ | |
3961 | widest_integer_literal_type_node | |
3962 | = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
dc24ddbd | 3963 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, |
3964 | widest_integer_literal_type_node)); | |
174fcc61 | 3965 | |
3966 | widest_unsigned_literal_type_node | |
3967 | = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
dc24ddbd | 3968 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, |
3969 | widest_unsigned_literal_type_node)); | |
174fcc61 | 3970 | |
3971 | /* `unsigned long' is the standard type for sizeof. | |
3972 | Note that stddef.h uses `unsigned long', | |
3973 | and this must agree, even if long and int are the same size. */ | |
654ef926 | 3974 | size_type_node = |
174fcc61 | 3975 | TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE))); |
654ef926 | 3976 | signed_size_type_node = c_common_signed_type (size_type_node); |
3977 | set_sizetype (size_type_node); | |
174fcc61 | 3978 | |
73673831 | 3979 | pid_type_node = |
3980 | TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE))); | |
3981 | ||
174fcc61 | 3982 | build_common_tree_nodes_2 (flag_short_double); |
3983 | ||
d946ea19 | 3984 | record_builtin_type (RID_FLOAT, NULL, float_type_node); |
3985 | record_builtin_type (RID_DOUBLE, NULL, double_type_node); | |
174fcc61 | 3986 | record_builtin_type (RID_MAX, "long double", long_double_type_node); |
3987 | ||
c4503c0a | 3988 | /* Only supported decimal floating point extension if the target |
3989 | actually supports underlying modes. */ | |
3990 | if (targetm.scalar_mode_supported_p (SDmode) | |
3991 | && targetm.scalar_mode_supported_p (DDmode) | |
3992 | && targetm.scalar_mode_supported_p (TDmode)) | |
3993 | { | |
3994 | record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node); | |
3995 | record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node); | |
3996 | record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node); | |
3997 | } | |
3998 | ||
9421ebb9 | 3999 | if (targetm.fixed_point_supported_p ()) |
4000 | { | |
4001 | record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node); | |
4002 | record_builtin_type (RID_FRACT, NULL, fract_type_node); | |
4003 | record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node); | |
4004 | record_builtin_type (RID_MAX, "long long _Fract", | |
4005 | long_long_fract_type_node); | |
4006 | record_builtin_type (RID_MAX, "unsigned short _Fract", | |
4007 | unsigned_short_fract_type_node); | |
4008 | record_builtin_type (RID_MAX, "unsigned _Fract", | |
4009 | unsigned_fract_type_node); | |
4010 | record_builtin_type (RID_MAX, "unsigned long _Fract", | |
4011 | unsigned_long_fract_type_node); | |
4012 | record_builtin_type (RID_MAX, "unsigned long long _Fract", | |
4013 | unsigned_long_long_fract_type_node); | |
4014 | record_builtin_type (RID_MAX, "_Sat short _Fract", | |
4015 | sat_short_fract_type_node); | |
4016 | record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node); | |
4017 | record_builtin_type (RID_MAX, "_Sat long _Fract", | |
4018 | sat_long_fract_type_node); | |
4019 | record_builtin_type (RID_MAX, "_Sat long long _Fract", | |
4020 | sat_long_long_fract_type_node); | |
4021 | record_builtin_type (RID_MAX, "_Sat unsigned short _Fract", | |
4022 | sat_unsigned_short_fract_type_node); | |
4023 | record_builtin_type (RID_MAX, "_Sat unsigned _Fract", | |
4024 | sat_unsigned_fract_type_node); | |
4025 | record_builtin_type (RID_MAX, "_Sat unsigned long _Fract", | |
4026 | sat_unsigned_long_fract_type_node); | |
4027 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract", | |
4028 | sat_unsigned_long_long_fract_type_node); | |
4029 | record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node); | |
4030 | record_builtin_type (RID_ACCUM, NULL, accum_type_node); | |
4031 | record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node); | |
4032 | record_builtin_type (RID_MAX, "long long _Accum", | |
4033 | long_long_accum_type_node); | |
4034 | record_builtin_type (RID_MAX, "unsigned short _Accum", | |
4035 | unsigned_short_accum_type_node); | |
4036 | record_builtin_type (RID_MAX, "unsigned _Accum", | |
4037 | unsigned_accum_type_node); | |
4038 | record_builtin_type (RID_MAX, "unsigned long _Accum", | |
4039 | unsigned_long_accum_type_node); | |
4040 | record_builtin_type (RID_MAX, "unsigned long long _Accum", | |
4041 | unsigned_long_long_accum_type_node); | |
4042 | record_builtin_type (RID_MAX, "_Sat short _Accum", | |
4043 | sat_short_accum_type_node); | |
4044 | record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node); | |
4045 | record_builtin_type (RID_MAX, "_Sat long _Accum", | |
4046 | sat_long_accum_type_node); | |
4047 | record_builtin_type (RID_MAX, "_Sat long long _Accum", | |
4048 | sat_long_long_accum_type_node); | |
4049 | record_builtin_type (RID_MAX, "_Sat unsigned short _Accum", | |
4050 | sat_unsigned_short_accum_type_node); | |
4051 | record_builtin_type (RID_MAX, "_Sat unsigned _Accum", | |
4052 | sat_unsigned_accum_type_node); | |
4053 | record_builtin_type (RID_MAX, "_Sat unsigned long _Accum", | |
4054 | sat_unsigned_long_accum_type_node); | |
4055 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum", | |
4056 | sat_unsigned_long_long_accum_type_node); | |
4057 | ||
4058 | } | |
4059 | ||
dc24ddbd | 4060 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, |
4061 | get_identifier ("complex int"), | |
4062 | complex_integer_type_node)); | |
4063 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, | |
4064 | get_identifier ("complex float"), | |
4065 | complex_float_type_node)); | |
4066 | lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, | |
4067 | get_identifier ("complex double"), | |
4068 | complex_double_type_node)); | |
4069 | lang_hooks.decls.pushdecl | |
20325f61 | 4070 | (build_decl (TYPE_DECL, get_identifier ("complex long double"), |
4071 | complex_long_double_type_node)); | |
174fcc61 | 4072 | |
e256d445 | 4073 | if (c_dialect_cxx ()) |
4074 | /* For C++, make fileptr_type_node a distinct void * type until | |
4075 | FILE type is defined. */ | |
e086912e | 4076 | fileptr_type_node = build_variant_type_copy (ptr_type_node); |
e256d445 | 4077 | |
d946ea19 | 4078 | record_builtin_type (RID_VOID, NULL, void_type_node); |
174fcc61 | 4079 | |
6753bca0 | 4080 | /* Set the TYPE_NAME for any variants that were built before |
4081 | record_builtin_type gave names to the built-in types. */ | |
4082 | { | |
4083 | tree void_name = TYPE_NAME (void_type_node); | |
4084 | TYPE_NAME (void_type_node) = NULL_TREE; | |
4085 | TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST)) | |
4086 | = void_name; | |
4087 | TYPE_NAME (void_type_node) = void_name; | |
4088 | } | |
4089 | ||
fbf0afd1 | 4090 | /* This node must not be shared. */ |
7c446c95 | 4091 | void_zero_node = make_node (INTEGER_CST); |
d2d4bdde | 4092 | TREE_TYPE (void_zero_node) = void_type_node; |
4093 | ||
174fcc61 | 4094 | void_list_node = build_void_list_node (); |
4095 | ||
4096 | /* Make a type to be the domain of a few array types | |
4097 | whose domains don't really matter. | |
4098 | 200 is small enough that it always fits in size_t | |
4099 | and large enough that it can hold most function names for the | |
4100 | initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
4101 | array_domain_type = build_index_type (size_int (200)); | |
4102 | ||
4103 | /* Make a type for arrays of characters. | |
4104 | With luck nothing will ever really depend on the length of this | |
4105 | array type. */ | |
4106 | char_array_type_node | |
4107 | = build_array_type (char_type_node, array_domain_type); | |
4108 | ||
4109 | /* Likewise for arrays of ints. */ | |
4110 | int_array_type_node | |
4111 | = build_array_type (integer_type_node, array_domain_type); | |
4112 | ||
d2d4bdde | 4113 | string_type_node = build_pointer_type (char_type_node); |
4114 | const_string_type_node | |
4115 | = build_pointer_type (build_qualified_type | |
4116 | (char_type_node, TYPE_QUAL_CONST)); | |
4117 | ||
174fcc61 | 4118 | /* This is special for C++ so functions can be overloaded. */ |
18ef7ac2 | 4119 | wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE); |
174fcc61 | 4120 | wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); |
4121 | wchar_type_size = TYPE_PRECISION (wchar_type_node); | |
c0f19401 | 4122 | if (c_dialect_cxx ()) |
174fcc61 | 4123 | { |
78a8ed03 | 4124 | if (TYPE_UNSIGNED (wchar_type_node)) |
174fcc61 | 4125 | wchar_type_node = make_unsigned_type (wchar_type_size); |
4126 | else | |
4127 | wchar_type_node = make_signed_type (wchar_type_size); | |
4128 | record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); | |
4129 | } | |
4130 | else | |
4131 | { | |
4070745f | 4132 | signed_wchar_type_node = c_common_signed_type (wchar_type_node); |
11773141 | 4133 | unsigned_wchar_type_node = c_common_unsigned_type (wchar_type_node); |
174fcc61 | 4134 | } |
4135 | ||
4136 | /* This is for wide string constants. */ | |
4137 | wchar_array_type_node | |
4138 | = build_array_type (wchar_type_node, array_domain_type); | |
4139 | ||
924bbf02 | 4140 | /* Define 'char16_t'. */ |
4141 | char16_type_node = get_identifier (CHAR16_TYPE); | |
4142 | char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node)); | |
4143 | char16_type_size = TYPE_PRECISION (char16_type_node); | |
4144 | if (c_dialect_cxx ()) | |
4145 | { | |
4146 | char16_type_node = make_unsigned_type (char16_type_size); | |
4147 | ||
4148 | if (cxx_dialect == cxx0x) | |
4149 | record_builtin_type (RID_CHAR16, "char16_t", char16_type_node); | |
4150 | } | |
4151 | ||
4152 | /* This is for UTF-16 string constants. */ | |
4153 | char16_array_type_node | |
4154 | = build_array_type (char16_type_node, array_domain_type); | |
4155 | ||
4156 | /* Define 'char32_t'. */ | |
4157 | char32_type_node = get_identifier (CHAR32_TYPE); | |
4158 | char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node)); | |
4159 | char32_type_size = TYPE_PRECISION (char32_type_node); | |
4160 | if (c_dialect_cxx ()) | |
4161 | { | |
4162 | char32_type_node = make_unsigned_type (char32_type_size); | |
4163 | ||
4164 | if (cxx_dialect == cxx0x) | |
4165 | record_builtin_type (RID_CHAR32, "char32_t", char32_type_node); | |
4166 | } | |
4167 | ||
4168 | /* This is for UTF-32 string constants. */ | |
4169 | char32_array_type_node | |
4170 | = build_array_type (char32_type_node, array_domain_type); | |
4171 | ||
6bf5ed8d | 4172 | wint_type_node = |
4173 | TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); | |
4174 | ||
4175 | intmax_type_node = | |
4176 | TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); | |
4177 | uintmax_type_node = | |
4178 | TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); | |
4179 | ||
4180 | default_function_type = build_function_type (integer_type_node, NULL_TREE); | |
4181 | ptrdiff_type_node | |
4182 | = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); | |
11773141 | 4183 | unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node); |
6bf5ed8d | 4184 | |
dc24ddbd | 4185 | lang_hooks.decls.pushdecl |
20325f61 | 4186 | (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"), |
4187 | va_list_type_node)); | |
5f57a8b1 | 4188 | #ifdef TARGET_ENUM_VA_LIST |
4189 | { | |
4190 | int l; | |
4191 | const char *pname; | |
4192 | tree ptype; | |
4193 | for (l = 0; TARGET_ENUM_VA_LIST (l, &pname, &ptype); ++l) | |
4194 | { | |
4195 | lang_hooks.decls.pushdecl | |
4196 | (build_decl (TYPE_DECL, get_identifier (pname), | |
4197 | ptype)); | |
4198 | ||
4199 | } | |
4200 | } | |
4201 | #endif | |
8a15c04a | 4202 | |
8a15c04a | 4203 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
2d47cc32 | 4204 | { |
4205 | va_list_arg_type_node = va_list_ref_type_node = | |
4206 | build_pointer_type (TREE_TYPE (va_list_type_node)); | |
4207 | } | |
8a15c04a | 4208 | else |
2d47cc32 | 4209 | { |
4210 | va_list_arg_type_node = va_list_type_node; | |
4211 | va_list_ref_type_node = build_reference_type (va_list_type_node); | |
4212 | } | |
1cae46be | 4213 | |
dce22712 | 4214 | if (!flag_preprocess_only) |
4215 | c_define_builtins (va_list_ref_type_node, va_list_arg_type_node); | |
ffa8918b | 4216 | |
5c62f199 | 4217 | main_identifier_node = get_identifier ("main"); |
ae84079f | 4218 | |
4219 | /* Create the built-in __null node. It is important that this is | |
4220 | not shared. */ | |
4221 | null_node = make_node (INTEGER_CST); | |
4222 | TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0); | |
27213ba3 | 4223 | |
4224 | /* Since builtin_types isn't gc'ed, don't export these nodes. */ | |
4225 | memset (builtin_types, 0, sizeof (builtin_types)); | |
72040e7e | 4226 | } |
a66c9326 | 4227 | |
46a7e216 | 4228 | /* Look up the function in built_in_decls that corresponds to DECL |
4229 | and set ASMSPEC as its user assembler name. DECL must be a | |
f7f07c95 | 4230 | function decl that declares a builtin. */ |
46a7e216 | 4231 | |
4232 | void | |
4233 | set_builtin_user_assembler_name (tree decl, const char *asmspec) | |
4234 | { | |
4235 | tree builtin; | |
4236 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL | |
4237 | && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL | |
4238 | && asmspec != 0); | |
4239 | ||
4240 | builtin = built_in_decls [DECL_FUNCTION_CODE (decl)]; | |
4241 | set_user_assembler_name (builtin, asmspec); | |
4242 | if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY) | |
4243 | init_block_move_fn (asmspec); | |
4244 | else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET) | |
4245 | init_block_clear_fn (asmspec); | |
4246 | } | |
4247 | ||
79b01846 | 4248 | /* The number of named compound-literals generated thus far. */ |
4249 | static GTY(()) int compound_literal_number; | |
4250 | ||
4251 | /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */ | |
4252 | ||
4253 | void | |
4254 | set_compound_literal_name (tree decl) | |
4255 | { | |
4256 | char *name; | |
4257 | ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", | |
4258 | compound_literal_number); | |
4259 | compound_literal_number++; | |
4260 | DECL_NAME (decl) = get_identifier (name); | |
4261 | } | |
4262 | ||
a66c9326 | 4263 | tree |
1cae46be | 4264 | build_va_arg (tree expr, tree type) |
a66c9326 | 4265 | { |
4266 | return build1 (VA_ARG_EXPR, type, expr); | |
4267 | } | |
0d4238dc | 4268 | |
4269 | ||
dd878098 | 4270 | /* Linked list of disabled built-in functions. */ |
4271 | ||
4272 | typedef struct disabled_builtin | |
4273 | { | |
4274 | const char *name; | |
4275 | struct disabled_builtin *next; | |
4276 | } disabled_builtin; | |
4277 | static disabled_builtin *disabled_builtins = NULL; | |
4278 | ||
1cae46be | 4279 | static bool builtin_function_disabled_p (const char *); |
dd878098 | 4280 | |
4281 | /* Disable a built-in function specified by -fno-builtin-NAME. If NAME | |
4282 | begins with "__builtin_", give an error. */ | |
4283 | ||
4284 | void | |
1cae46be | 4285 | disable_builtin_function (const char *name) |
dd878098 | 4286 | { |
4287 | if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0) | |
b0b1af64 | 4288 | error ("cannot disable built-in function %qs", name); |
dd878098 | 4289 | else |
4290 | { | |
e85905e5 | 4291 | disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin); |
4292 | new_disabled_builtin->name = name; | |
4293 | new_disabled_builtin->next = disabled_builtins; | |
4294 | disabled_builtins = new_disabled_builtin; | |
dd878098 | 4295 | } |
4296 | } | |
4297 | ||
4298 | ||
4299 | /* Return true if the built-in function NAME has been disabled, false | |
4300 | otherwise. */ | |
4301 | ||
4302 | static bool | |
1cae46be | 4303 | builtin_function_disabled_p (const char *name) |
dd878098 | 4304 | { |
4305 | disabled_builtin *p; | |
4306 | for (p = disabled_builtins; p != NULL; p = p->next) | |
4307 | { | |
4308 | if (strcmp (name, p->name) == 0) | |
4309 | return true; | |
4310 | } | |
4311 | return false; | |
4312 | } | |
4313 | ||
4314 | ||
3237155d | 4315 | /* Worker for DEF_BUILTIN. |
4316 | Possibly define a builtin function with one or two names. | |
4317 | Does not declare a non-__builtin_ function if flag_no_builtin, or if | |
4318 | nonansi_p and flag_no_nonansi_builtin. */ | |
0d4238dc | 4319 | |
3237155d | 4320 | static void |
4321 | def_builtin_1 (enum built_in_function fncode, | |
4322 | const char *name, | |
4323 | enum built_in_class fnclass, | |
4324 | tree fntype, tree libtype, | |
4325 | bool both_p, bool fallback_p, bool nonansi_p, | |
4326 | tree fnattrs, bool implicit_p) | |
0d4238dc | 4327 | { |
3237155d | 4328 | tree decl; |
4329 | const char *libname; | |
4330 | ||
27213ba3 | 4331 | if (fntype == error_mark_node) |
4332 | return; | |
4333 | ||
3237155d | 4334 | gcc_assert ((!both_p && !fallback_p) |
4335 | || !strncmp (name, "__builtin_", | |
4336 | strlen ("__builtin_"))); | |
4337 | ||
4338 | libname = name + strlen ("__builtin_"); | |
54be5d7e | 4339 | decl = add_builtin_function (name, fntype, fncode, fnclass, |
4340 | (fallback_p ? libname : NULL), | |
4341 | fnattrs); | |
3237155d | 4342 | if (both_p |
4343 | && !flag_no_builtin && !builtin_function_disabled_p (libname) | |
dd878098 | 4344 | && !(nonansi_p && flag_no_nonansi_builtin)) |
54be5d7e | 4345 | add_builtin_function (libname, libtype, fncode, fnclass, |
4346 | NULL, fnattrs); | |
a40609e6 | 4347 | |
3237155d | 4348 | built_in_decls[(int) fncode] = decl; |
4349 | if (implicit_p) | |
4350 | implicit_built_in_decls[(int) fncode] = decl; | |
0d4238dc | 4351 | } |
e94026da | 4352 | \f |
d7aeef06 | 4353 | /* Nonzero if the type T promotes to int. This is (nearly) the |
4354 | integral promotions defined in ISO C99 6.3.1.1/2. */ | |
4355 | ||
4356 | bool | |
9f627b1a | 4357 | c_promoting_integer_type_p (const_tree t) |
d7aeef06 | 4358 | { |
4359 | switch (TREE_CODE (t)) | |
4360 | { | |
4361 | case INTEGER_TYPE: | |
4362 | return (TYPE_MAIN_VARIANT (t) == char_type_node | |
4363 | || TYPE_MAIN_VARIANT (t) == signed_char_type_node | |
4364 | || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node | |
4365 | || TYPE_MAIN_VARIANT (t) == short_integer_type_node | |
7aa1e6eb | 4366 | || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node |
4367 | || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node)); | |
d7aeef06 | 4368 | |
4369 | case ENUMERAL_TYPE: | |
4370 | /* ??? Technically all enumerations not larger than an int | |
4371 | promote to an int. But this is used along code paths | |
4372 | that only want to notice a size change. */ | |
4373 | return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node); | |
4374 | ||
4375 | case BOOLEAN_TYPE: | |
4376 | return 1; | |
4377 | ||
4378 | default: | |
4379 | return 0; | |
4380 | } | |
4381 | } | |
4382 | ||
e94026da | 4383 | /* Return 1 if PARMS specifies a fixed number of parameters |
4384 | and none of their types is affected by default promotions. */ | |
4385 | ||
4386 | int | |
9f627b1a | 4387 | self_promoting_args_p (const_tree parms) |
e94026da | 4388 | { |
9f627b1a | 4389 | const_tree t; |
e94026da | 4390 | for (t = parms; t; t = TREE_CHAIN (t)) |
4391 | { | |
19cb6b50 | 4392 | tree type = TREE_VALUE (t); |
43f74bc4 | 4393 | |
e1d8e198 | 4394 | if (type == error_mark_node) |
4395 | continue; | |
4396 | ||
e94026da | 4397 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
4398 | return 0; | |
4399 | ||
4400 | if (type == 0) | |
4401 | return 0; | |
4402 | ||
4403 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
4404 | return 0; | |
4405 | ||
d7aeef06 | 4406 | if (c_promoting_integer_type_p (type)) |
e94026da | 4407 | return 0; |
4408 | } | |
4409 | return 1; | |
4410 | } | |
605fb01e | 4411 | |
c10de5e7 | 4412 | /* Recursively remove any '*' or '&' operator from TYPE. */ |
4413 | tree | |
4414 | strip_pointer_operator (tree t) | |
4415 | { | |
4416 | while (POINTER_TYPE_P (t)) | |
4417 | t = TREE_TYPE (t); | |
4418 | return t; | |
4419 | } | |
4420 | ||
57a0ed23 | 4421 | /* Recursively remove pointer or array type from TYPE. */ |
4422 | tree | |
4423 | strip_pointer_or_array_types (tree t) | |
4424 | { | |
4425 | while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t)) | |
4426 | t = TREE_TYPE (t); | |
4427 | return t; | |
4428 | } | |
4429 | ||
e41f0d80 | 4430 | /* Used to compare case labels. K1 and K2 are actually tree nodes |
4431 | representing case labels, or NULL_TREE for a `default' label. | |
4432 | Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after | |
4433 | K2, and 0 if K1 and K2 are equal. */ | |
4434 | ||
4435 | int | |
1cae46be | 4436 | case_compare (splay_tree_key k1, splay_tree_key k2) |
e41f0d80 | 4437 | { |
4438 | /* Consider a NULL key (such as arises with a `default' label) to be | |
4439 | smaller than anything else. */ | |
4440 | if (!k1) | |
4441 | return k2 ? -1 : 0; | |
4442 | else if (!k2) | |
4443 | return k1 ? 1 : 0; | |
4444 | ||
4445 | return tree_int_cst_compare ((tree) k1, (tree) k2); | |
4446 | } | |
4447 | ||
4448 | /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If | |
4449 | LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is | |
4450 | actually a `default' label. If only HIGH_VALUE is NULL_TREE, then | |
4451 | case label was declared using the usual C/C++ syntax, rather than | |
4452 | the GNU case range extension. CASES is a tree containing all the | |
4453 | case ranges processed so far; COND is the condition for the | |
eaae3b75 | 4454 | switch-statement itself. Returns the CASE_LABEL_EXPR created, or |
4455 | ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */ | |
e41f0d80 | 4456 | |
4457 | tree | |
2ca392fd | 4458 | c_add_case_label (splay_tree cases, tree cond, tree orig_type, |
4459 | tree low_value, tree high_value) | |
e41f0d80 | 4460 | { |
4461 | tree type; | |
4462 | tree label; | |
4463 | tree case_label; | |
4464 | splay_tree_node node; | |
4465 | ||
4466 | /* Create the LABEL_DECL itself. */ | |
4ee9c684 | 4467 | label = create_artificial_label (); |
e41f0d80 | 4468 | |
4469 | /* If there was an error processing the switch condition, bail now | |
4470 | before we get more confused. */ | |
4471 | if (!cond || cond == error_mark_node) | |
4ee9c684 | 4472 | goto error_out; |
e41f0d80 | 4473 | |
1cae46be | 4474 | if ((low_value && TREE_TYPE (low_value) |
4475 | && POINTER_TYPE_P (TREE_TYPE (low_value))) | |
e41f0d80 | 4476 | || (high_value && TREE_TYPE (high_value) |
4477 | && POINTER_TYPE_P (TREE_TYPE (high_value)))) | |
b96dc121 | 4478 | { |
4479 | error ("pointers are not permitted as case values"); | |
4480 | goto error_out; | |
4481 | } | |
e41f0d80 | 4482 | |
4483 | /* Case ranges are a GNU extension. */ | |
8864917d | 4484 | if (high_value) |
4485 | pedwarn (OPT_pedantic, | |
4486 | "range expressions in switch statements are non-standard"); | |
e41f0d80 | 4487 | |
4488 | type = TREE_TYPE (cond); | |
4489 | if (low_value) | |
4490 | { | |
4491 | low_value = check_case_value (low_value); | |
4492 | low_value = convert_and_check (type, low_value); | |
96722196 | 4493 | if (low_value == error_mark_node) |
4494 | goto error_out; | |
e41f0d80 | 4495 | } |
4496 | if (high_value) | |
4497 | { | |
4498 | high_value = check_case_value (high_value); | |
4499 | high_value = convert_and_check (type, high_value); | |
96722196 | 4500 | if (high_value == error_mark_node) |
4501 | goto error_out; | |
e41f0d80 | 4502 | } |
4503 | ||
96722196 | 4504 | if (low_value && high_value) |
4505 | { | |
4506 | /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't | |
a0c938f0 | 4507 | really a case range, even though it was written that way. |
4508 | Remove the HIGH_VALUE to simplify later processing. */ | |
96722196 | 4509 | if (tree_int_cst_equal (low_value, high_value)) |
4510 | high_value = NULL_TREE; | |
4511 | else if (!tree_int_cst_lt (low_value, high_value)) | |
4512 | warning (0, "empty range specified"); | |
4513 | } | |
e41f0d80 | 4514 | |
2ca392fd | 4515 | /* See if the case is in range of the type of the original testing |
4516 | expression. If both low_value and high_value are out of range, | |
4517 | don't insert the case label and return NULL_TREE. */ | |
4518 | if (low_value | |
84166705 | 4519 | && !check_case_bounds (type, orig_type, |
4520 | &low_value, high_value ? &high_value : NULL)) | |
2ca392fd | 4521 | return NULL_TREE; |
4522 | ||
e41f0d80 | 4523 | /* Look up the LOW_VALUE in the table of case labels we already |
4524 | have. */ | |
4525 | node = splay_tree_lookup (cases, (splay_tree_key) low_value); | |
4526 | /* If there was not an exact match, check for overlapping ranges. | |
4527 | There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; | |
4528 | that's a `default' label and the only overlap is an exact match. */ | |
4529 | if (!node && (low_value || high_value)) | |
4530 | { | |
4531 | splay_tree_node low_bound; | |
4532 | splay_tree_node high_bound; | |
4533 | ||
4534 | /* Even though there wasn't an exact match, there might be an | |
4535 | overlap between this case range and another case range. | |
4536 | Since we've (inductively) not allowed any overlapping case | |
4537 | ranges, we simply need to find the greatest low case label | |
4538 | that is smaller that LOW_VALUE, and the smallest low case | |
4539 | label that is greater than LOW_VALUE. If there is an overlap | |
4540 | it will occur in one of these two ranges. */ | |
4541 | low_bound = splay_tree_predecessor (cases, | |
4542 | (splay_tree_key) low_value); | |
4543 | high_bound = splay_tree_successor (cases, | |
4544 | (splay_tree_key) low_value); | |
4545 | ||
4546 | /* Check to see if the LOW_BOUND overlaps. It is smaller than | |
4547 | the LOW_VALUE, so there is no need to check unless the | |
4548 | LOW_BOUND is in fact itself a case range. */ | |
4549 | if (low_bound | |
4550 | && CASE_HIGH ((tree) low_bound->value) | |
4551 | && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), | |
4552 | low_value) >= 0) | |
4553 | node = low_bound; | |
4554 | /* Check to see if the HIGH_BOUND overlaps. The low end of that | |
4555 | range is bigger than the low end of the current range, so we | |
4556 | are only interested if the current range is a real range, and | |
4557 | not an ordinary case label. */ | |
1cae46be | 4558 | else if (high_bound |
e41f0d80 | 4559 | && high_value |
4560 | && (tree_int_cst_compare ((tree) high_bound->key, | |
4561 | high_value) | |
4562 | <= 0)) | |
4563 | node = high_bound; | |
4564 | } | |
4565 | /* If there was an overlap, issue an error. */ | |
4566 | if (node) | |
4567 | { | |
eaae3b75 | 4568 | tree duplicate = CASE_LABEL ((tree) node->value); |
e41f0d80 | 4569 | |
4570 | if (high_value) | |
4571 | { | |
4572 | error ("duplicate (or overlapping) case value"); | |
9bc3739f | 4573 | error ("%Jthis is the first entry overlapping that value", duplicate); |
e41f0d80 | 4574 | } |
4575 | else if (low_value) | |
4576 | { | |
4577 | error ("duplicate case value") ; | |
9bc3739f | 4578 | error ("%Jpreviously used here", duplicate); |
e41f0d80 | 4579 | } |
4580 | else | |
4581 | { | |
4582 | error ("multiple default labels in one switch"); | |
9bc3739f | 4583 | error ("%Jthis is the first default label", duplicate); |
e41f0d80 | 4584 | } |
4ee9c684 | 4585 | goto error_out; |
e41f0d80 | 4586 | } |
4587 | ||
4588 | /* Add a CASE_LABEL to the statement-tree. */ | |
4589 | case_label = add_stmt (build_case_label (low_value, high_value, label)); | |
4590 | /* Register this case label in the splay tree. */ | |
1cae46be | 4591 | splay_tree_insert (cases, |
e41f0d80 | 4592 | (splay_tree_key) low_value, |
4593 | (splay_tree_value) case_label); | |
4594 | ||
4595 | return case_label; | |
4ee9c684 | 4596 | |
4597 | error_out: | |
daf6dff5 | 4598 | /* Add a label so that the back-end doesn't think that the beginning of |
4ee9c684 | 4599 | the switch is unreachable. Note that we do not add a case label, as |
a53ff4c1 | 4600 | that just leads to duplicates and thence to failure later on. */ |
4ee9c684 | 4601 | if (!cases->root) |
4602 | { | |
4603 | tree t = create_artificial_label (); | |
daf6dff5 | 4604 | add_stmt (build_stmt (LABEL_EXPR, t)); |
4ee9c684 | 4605 | } |
4606 | return error_mark_node; | |
4607 | } | |
4608 | ||
4609 | /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. | |
4610 | Used to verify that case values match up with enumerator values. */ | |
4611 | ||
4612 | static void | |
4613 | match_case_to_enum_1 (tree key, tree type, tree label) | |
4614 | { | |
4615 | char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1]; | |
4616 | ||
4617 | /* ??? Not working too hard to print the double-word value. | |
4618 | Should perhaps be done with %lwd in the diagnostic routines? */ | |
4619 | if (TREE_INT_CST_HIGH (key) == 0) | |
4620 | snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED, | |
4621 | TREE_INT_CST_LOW (key)); | |
4622 | else if (!TYPE_UNSIGNED (type) | |
4623 | && TREE_INT_CST_HIGH (key) == -1 | |
4624 | && TREE_INT_CST_LOW (key) != 0) | |
4625 | snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED, | |
4626 | -TREE_INT_CST_LOW (key)); | |
4627 | else | |
4628 | snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX, | |
7df7561b | 4629 | (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key), |
4630 | (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key)); | |
4ee9c684 | 4631 | |
4632 | if (TYPE_NAME (type) == 0) | |
6cbbbc89 | 4633 | warning (warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, |
4634 | "%Jcase value %qs not in enumerated type", | |
eaae3b75 | 4635 | CASE_LABEL (label), buf); |
4ee9c684 | 4636 | else |
6cbbbc89 | 4637 | warning (warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, |
4638 | "%Jcase value %qs not in enumerated type %qT", | |
eaae3b75 | 4639 | CASE_LABEL (label), buf, type); |
4ee9c684 | 4640 | } |
4641 | ||
359d87c6 | 4642 | /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. |
4643 | Used to verify that case values match up with enumerator values. */ | |
4644 | ||
4ee9c684 | 4645 | static int |
4646 | match_case_to_enum (splay_tree_node node, void *data) | |
4647 | { | |
4648 | tree label = (tree) node->value; | |
4fd61bc6 | 4649 | tree type = (tree) data; |
4ee9c684 | 4650 | |
4651 | /* Skip default case. */ | |
4652 | if (!CASE_LOW (label)) | |
4653 | return 0; | |
4654 | ||
359d87c6 | 4655 | /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear |
4ee9c684 | 4656 | when we did our enum->case scan. Reset our scratch bit after. */ |
359d87c6 | 4657 | if (!CASE_LOW_SEEN (label)) |
4ee9c684 | 4658 | match_case_to_enum_1 (CASE_LOW (label), type, label); |
4659 | else | |
359d87c6 | 4660 | CASE_LOW_SEEN (label) = 0; |
4ee9c684 | 4661 | |
359d87c6 | 4662 | /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is |
4663 | not set, that means that CASE_HIGH did not appear when we did our | |
4664 | enum->case scan. Reset our scratch bit after. */ | |
4ee9c684 | 4665 | if (CASE_HIGH (label)) |
4666 | { | |
359d87c6 | 4667 | if (!CASE_HIGH_SEEN (label)) |
4668 | match_case_to_enum_1 (CASE_HIGH (label), type, label); | |
4669 | else | |
4670 | CASE_HIGH_SEEN (label) = 0; | |
4ee9c684 | 4671 | } |
4672 | ||
4673 | return 0; | |
4674 | } | |
4675 | ||
e7911019 | 4676 | /* Handle -Wswitch*. Called from the front end after parsing the |
4677 | switch construct. */ | |
4678 | /* ??? Should probably be somewhere generic, since other languages | |
4679 | besides C and C++ would want this. At the moment, however, C/C++ | |
4680 | are the only tree-ssa languages that support enumerations at all, | |
4681 | so the point is moot. */ | |
4ee9c684 | 4682 | |
e7911019 | 4683 | void |
4684 | c_do_switch_warnings (splay_tree cases, location_t switch_location, | |
4685 | tree type, tree cond) | |
4ee9c684 | 4686 | { |
b27ac6b5 | 4687 | splay_tree_node default_node; |
359d87c6 | 4688 | splay_tree_node node; |
4689 | tree chain; | |
6cbbbc89 | 4690 | int saved_warn_switch; |
4ee9c684 | 4691 | |
4692 | if (!warn_switch && !warn_switch_enum && !warn_switch_default) | |
4693 | return; | |
4694 | ||
4ee9c684 | 4695 | default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); |
8b6866af | 4696 | if (!default_node) |
4697 | warning (OPT_Wswitch_default, "%Hswitch missing default case", | |
4698 | &switch_location); | |
4ee9c684 | 4699 | |
359d87c6 | 4700 | /* From here on, we only care about about enumerated types. */ |
4701 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
4702 | return; | |
4703 | ||
4ee9c684 | 4704 | /* If the switch expression was an enumerated type, check that |
4705 | exactly all enumeration literals are covered by the cases. | |
4706 | The check is made when -Wswitch was specified and there is no | |
4707 | default case, or when -Wswitch-enum was specified. */ | |
4ee9c684 | 4708 | |
359d87c6 | 4709 | if (!warn_switch_enum |
4710 | && !(warn_switch && !default_node)) | |
4711 | return; | |
4712 | ||
4713 | /* Clearing COND if it is not an integer constant simplifies | |
4714 | the tests inside the loop below. */ | |
4715 | if (TREE_CODE (cond) != INTEGER_CST) | |
4716 | cond = NULL_TREE; | |
4717 | ||
4718 | /* The time complexity here is O(N*lg(N)) worst case, but for the | |
4719 | common case of monotonically increasing enumerators, it is | |
4720 | O(N), since the nature of the splay tree will keep the next | |
4721 | element adjacent to the root at all times. */ | |
4ee9c684 | 4722 | |
359d87c6 | 4723 | for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) |
4724 | { | |
4725 | tree value = TREE_VALUE (chain); | |
4726 | node = splay_tree_lookup (cases, (splay_tree_key) value); | |
4727 | if (node) | |
4ee9c684 | 4728 | { |
359d87c6 | 4729 | /* Mark the CASE_LOW part of the case entry as seen. */ |
4730 | tree label = (tree) node->value; | |
4731 | CASE_LOW_SEEN (label) = 1; | |
4732 | continue; | |
4733 | } | |
4734 | ||
4735 | /* Even though there wasn't an exact match, there might be a | |
f0b5f617 | 4736 | case range which includes the enumerator's value. */ |
359d87c6 | 4737 | node = splay_tree_predecessor (cases, (splay_tree_key) value); |
4738 | if (node && CASE_HIGH ((tree) node->value)) | |
4739 | { | |
4740 | tree label = (tree) node->value; | |
4741 | int cmp = tree_int_cst_compare (CASE_HIGH (label), value); | |
4742 | if (cmp >= 0) | |
4ee9c684 | 4743 | { |
359d87c6 | 4744 | /* If we match the upper bound exactly, mark the CASE_HIGH |
4745 | part of the case entry as seen. */ | |
4746 | if (cmp == 0) | |
4747 | CASE_HIGH_SEEN (label) = 1; | |
4748 | continue; | |
4ee9c684 | 4749 | } |
4750 | } | |
4751 | ||
359d87c6 | 4752 | /* We've now determined that this enumerated literal isn't |
4753 | handled by the case labels of the switch statement. */ | |
4ee9c684 | 4754 | |
359d87c6 | 4755 | /* If the switch expression is a constant, we only really care |
4756 | about whether that constant is handled by the switch. */ | |
4757 | if (cond && tree_int_cst_compare (cond, value)) | |
4758 | continue; | |
4ee9c684 | 4759 | |
6cbbbc89 | 4760 | /* If there is a default_node, the only relevant option is |
4761 | Wswitch-enum. Otherwise, if both are enabled then we prefer | |
4762 | to warn using -Wswitch because -Wswitch is enabled by -Wall | |
4763 | while -Wswitch-enum is explicit. */ | |
4764 | warning ((default_node || !warn_switch) | |
4765 | ? OPT_Wswitch_enum : OPT_Wswitch, | |
4766 | "%Henumeration value %qE not handled in switch", | |
359d87c6 | 4767 | &switch_location, TREE_PURPOSE (chain)); |
4ee9c684 | 4768 | } |
359d87c6 | 4769 | |
4770 | /* Warn if there are case expressions that don't correspond to | |
4771 | enumerators. This can occur since C and C++ don't enforce | |
4772 | type-checking of assignments to enumeration variables. | |
4773 | ||
4774 | The time complexity here is now always O(N) worst case, since | |
4775 | we should have marked both the lower bound and upper bound of | |
4776 | every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN | |
4777 | above. This scan also resets those fields. */ | |
6cbbbc89 | 4778 | |
4779 | /* If there is a default_node, the only relevant option is | |
4780 | Wswitch-enum. Otherwise, if both are enabled then we prefer | |
4781 | to warn using -Wswitch because -Wswitch is enabled by -Wall | |
4782 | while -Wswitch-enum is explicit. */ | |
4783 | saved_warn_switch = warn_switch; | |
4784 | if (default_node) | |
4785 | warn_switch = 0; | |
359d87c6 | 4786 | splay_tree_foreach (cases, match_case_to_enum, type); |
6cbbbc89 | 4787 | warn_switch = saved_warn_switch; |
4788 | ||
e41f0d80 | 4789 | } |
4790 | ||
9dd48740 | 4791 | /* Finish an expression taking the address of LABEL (an |
4792 | IDENTIFIER_NODE). Returns an expression for the address. */ | |
d0a47c8d | 4793 | |
1cae46be | 4794 | tree |
4795 | finish_label_address_expr (tree label) | |
d0a47c8d | 4796 | { |
4797 | tree result; | |
4798 | ||
8864917d | 4799 | pedwarn (OPT_pedantic, "taking the address of a label is non-standard"); |
d0a47c8d | 4800 | |
9dd48740 | 4801 | if (label == error_mark_node) |
4802 | return error_mark_node; | |
4803 | ||
d0a47c8d | 4804 | label = lookup_label (label); |
4805 | if (label == NULL_TREE) | |
4806 | result = null_pointer_node; | |
4807 | else | |
4808 | { | |
4809 | TREE_USED (label) = 1; | |
4810 | result = build1 (ADDR_EXPR, ptr_type_node, label); | |
d0a47c8d | 4811 | /* The current function in not necessarily uninlinable. |
4812 | Computed gotos are incompatible with inlining, but the value | |
4813 | here could be used only in a diagnostic, for example. */ | |
4814 | } | |
4815 | ||
4816 | return result; | |
4817 | } | |
4818 | ||
e41f0d80 | 4819 | /* Hook used by expand_expr to expand language-specific tree codes. */ |
b27ac6b5 | 4820 | /* The only things that should go here are bits needed to expand |
365db11e | 4821 | constant initializers. Everything else should be handled by the |
4ee9c684 | 4822 | gimplification routines. */ |
e41f0d80 | 4823 | |
4824 | rtx | |
b27ac6b5 | 4825 | c_expand_expr (tree exp, rtx target, enum machine_mode tmode, |
7d339f93 | 4826 | int modifiera /* Actually enum expand_modifier. */, |
60ffaf4d | 4827 | rtx *alt_rtl) |
e41f0d80 | 4828 | { |
7d339f93 | 4829 | enum expand_modifier modifier = (enum expand_modifier) modifiera; |
e41f0d80 | 4830 | switch (TREE_CODE (exp)) |
4831 | { | |
ec11e38e | 4832 | case COMPOUND_LITERAL_EXPR: |
4833 | { | |
4834 | /* Initialize the anonymous variable declared in the compound | |
4835 | literal, then return the variable. */ | |
4836 | tree decl = COMPOUND_LITERAL_EXPR_DECL (exp); | |
4837 | emit_local_var (decl); | |
60ffaf4d | 4838 | return expand_expr_real (decl, target, tmode, modifier, alt_rtl); |
ec11e38e | 4839 | } |
4840 | ||
e41f0d80 | 4841 | default: |
231bd014 | 4842 | gcc_unreachable (); |
e41f0d80 | 4843 | } |
e41f0d80 | 4844 | } |
4845 | ||
cf484390 | 4846 | /* Hook used by staticp to handle language-specific tree codes. */ |
4847 | ||
4848 | tree | |
4849 | c_staticp (tree exp) | |
4850 | { | |
4851 | return (TREE_CODE (exp) == COMPOUND_LITERAL_EXPR | |
4852 | && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)) | |
4853 | ? exp : NULL); | |
4854 | } | |
4f9a1c9b | 4855 | \f |
4856 | ||
4857 | /* Given a boolean expression ARG, return a tree representing an increment | |
4858 | or decrement (as indicated by CODE) of ARG. The front end must check for | |
4859 | invalid cases (e.g., decrement in C++). */ | |
4860 | tree | |
1cae46be | 4861 | boolean_increment (enum tree_code code, tree arg) |
4f9a1c9b | 4862 | { |
4863 | tree val; | |
69db191c | 4864 | tree true_res = build_int_cst (TREE_TYPE (arg), 1); |
c0f19401 | 4865 | |
4f9a1c9b | 4866 | arg = stabilize_reference (arg); |
4867 | switch (code) | |
4868 | { | |
4869 | case PREINCREMENT_EXPR: | |
14ae0310 | 4870 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 4871 | break; |
4872 | case POSTINCREMENT_EXPR: | |
14ae0310 | 4873 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 4874 | arg = save_expr (arg); |
14ae0310 | 4875 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
4876 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 4877 | break; |
4878 | case PREDECREMENT_EXPR: | |
14ae0310 | 4879 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
4880 | invert_truthvalue (arg)); | |
4f9a1c9b | 4881 | break; |
4882 | case POSTDECREMENT_EXPR: | |
14ae0310 | 4883 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
4884 | invert_truthvalue (arg)); | |
4f9a1c9b | 4885 | arg = save_expr (arg); |
14ae0310 | 4886 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
4887 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 4888 | break; |
4889 | default: | |
231bd014 | 4890 | gcc_unreachable (); |
4f9a1c9b | 4891 | } |
4892 | TREE_SIDE_EFFECTS (val) = 1; | |
4893 | return val; | |
4894 | } | |
76a6e674 | 4895 | \f |
fd6f6435 | 4896 | /* Built-in macros for stddef.h, that require macros defined in this |
4897 | file. */ | |
79cf3ec1 | 4898 | void |
1cae46be | 4899 | c_stddef_cpp_builtins(void) |
1ed9d5f5 | 4900 | { |
63994318 | 4901 | builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0); |
4902 | builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0); | |
4903 | builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0); | |
4904 | builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0); | |
36bccbfc | 4905 | builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0); |
4906 | builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0); | |
b0726616 | 4907 | builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0); |
4908 | builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0); | |
574006c3 | 4909 | } |
4910 | ||
7d3b509a | 4911 | static void |
1cae46be | 4912 | c_init_attributes (void) |
7d3b509a | 4913 | { |
4914 | /* Fill in the built_in_attributes array. */ | |
7c446c95 | 4915 | #define DEF_ATTR_NULL_TREE(ENUM) \ |
7d3b509a | 4916 | built_in_attributes[(int) ENUM] = NULL_TREE; |
7c446c95 | 4917 | #define DEF_ATTR_INT(ENUM, VALUE) \ |
7016c612 | 4918 | built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE); |
7d3b509a | 4919 | #define DEF_ATTR_IDENT(ENUM, STRING) \ |
4920 | built_in_attributes[(int) ENUM] = get_identifier (STRING); | |
4921 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \ | |
4922 | built_in_attributes[(int) ENUM] \ | |
4923 | = tree_cons (built_in_attributes[(int) PURPOSE], \ | |
4924 | built_in_attributes[(int) VALUE], \ | |
4925 | built_in_attributes[(int) CHAIN]); | |
7d3b509a | 4926 | #include "builtin-attrs.def" |
4927 | #undef DEF_ATTR_NULL_TREE | |
4928 | #undef DEF_ATTR_INT | |
4929 | #undef DEF_ATTR_IDENT | |
4930 | #undef DEF_ATTR_TREE_LIST | |
76a6e674 | 4931 | } |
5f3cead1 | 4932 | |
f8e93a2e | 4933 | /* Attribute handlers common to C front ends. */ |
4934 | ||
4935 | /* Handle a "packed" attribute; arguments as in | |
4936 | struct attribute_spec.handler. */ | |
4937 | ||
4938 | static tree | |
9a03a746 | 4939 | handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
09347743 | 4940 | int flags, bool *no_add_attrs) |
f8e93a2e | 4941 | { |
f40175cb | 4942 | if (TYPE_P (*node)) |
f8e93a2e | 4943 | { |
4944 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 4945 | *node = build_variant_type_copy (*node); |
f40175cb | 4946 | TYPE_PACKED (*node) = 1; |
f8e93a2e | 4947 | } |
4948 | else if (TREE_CODE (*node) == FIELD_DECL) | |
c2ab04f9 | 4949 | { |
4950 | if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT) | |
4951 | warning (OPT_Wattributes, | |
4952 | "%qE attribute ignored for field of type %qT", | |
4953 | name, TREE_TYPE (*node)); | |
4954 | else | |
4955 | DECL_PACKED (*node) = 1; | |
4956 | } | |
f8e93a2e | 4957 | /* We can't set DECL_PACKED for a VAR_DECL, because the bit is |
f40175cb | 4958 | used for DECL_REGISTER. It wouldn't mean anything anyway. |
4959 | We can't set DECL_PACKED on the type of a TYPE_DECL, because | |
4960 | that changes what the typedef is typing. */ | |
f8e93a2e | 4961 | else |
4962 | { | |
9b2d6d13 | 4963 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 4964 | *no_add_attrs = true; |
4965 | } | |
4966 | ||
4967 | return NULL_TREE; | |
4968 | } | |
4969 | ||
4970 | /* Handle a "nocommon" attribute; arguments as in | |
4971 | struct attribute_spec.handler. */ | |
4972 | ||
4973 | static tree | |
1cae46be | 4974 | handle_nocommon_attribute (tree *node, tree name, |
9a03a746 | 4975 | tree ARG_UNUSED (args), |
4976 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 4977 | { |
4978 | if (TREE_CODE (*node) == VAR_DECL) | |
4979 | DECL_COMMON (*node) = 0; | |
4980 | else | |
4981 | { | |
9b2d6d13 | 4982 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 4983 | *no_add_attrs = true; |
4984 | } | |
4985 | ||
4986 | return NULL_TREE; | |
4987 | } | |
4988 | ||
4989 | /* Handle a "common" attribute; arguments as in | |
4990 | struct attribute_spec.handler. */ | |
4991 | ||
4992 | static tree | |
9a03a746 | 4993 | handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
4994 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 4995 | { |
4996 | if (TREE_CODE (*node) == VAR_DECL) | |
4997 | DECL_COMMON (*node) = 1; | |
4998 | else | |
4999 | { | |
9b2d6d13 | 5000 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5001 | *no_add_attrs = true; |
5002 | } | |
5003 | ||
5004 | return NULL_TREE; | |
5005 | } | |
5006 | ||
5007 | /* Handle a "noreturn" attribute; arguments as in | |
5008 | struct attribute_spec.handler. */ | |
5009 | ||
5010 | static tree | |
9a03a746 | 5011 | handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
5012 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 5013 | { |
5014 | tree type = TREE_TYPE (*node); | |
5015 | ||
5016 | /* See FIXME comment in c_common_attribute_table. */ | |
5017 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5018 | TREE_THIS_VOLATILE (*node) = 1; | |
5019 | else if (TREE_CODE (type) == POINTER_TYPE | |
5020 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
5021 | TREE_TYPE (*node) | |
5022 | = build_pointer_type | |
5023 | (build_type_variant (TREE_TYPE (type), | |
71bda45d | 5024 | TYPE_READONLY (TREE_TYPE (type)), 1)); |
f8e93a2e | 5025 | else |
5026 | { | |
9b2d6d13 | 5027 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5028 | *no_add_attrs = true; |
5029 | } | |
5030 | ||
5031 | return NULL_TREE; | |
5032 | } | |
5033 | ||
5de92639 | 5034 | /* Handle a "hot" and attribute; arguments as in |
5035 | struct attribute_spec.handler. */ | |
5036 | ||
5037 | static tree | |
5038 | handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
46f8e3b0 | 5039 | int ARG_UNUSED (flags), bool *no_add_attrs) |
5de92639 | 5040 | { |
5041 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5042 | { | |
5043 | if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL) | |
5044 | { | |
5045 | warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", | |
5046 | name, "cold"); | |
5047 | *no_add_attrs = true; | |
5048 | } | |
46f8e3b0 | 5049 | else |
5050 | { | |
5051 | tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node); | |
5052 | ||
5053 | /* If we are not at -O3, but are optimizing, turn on -O3 | |
5054 | optimizations just for this one function. */ | |
5055 | if (((optimize > 0 && optimize < 3) || optimize_size) | |
a50eecc9 | 5056 | && targetm.target_option.hot_attribute_sets_optimization |
46f8e3b0 | 5057 | && (!old_opts || old_opts == optimization_default_node)) |
5058 | { | |
5059 | /* Create the hot optimization node if needed. */ | |
5060 | if (!optimization_hot_node) | |
5061 | { | |
5062 | struct cl_optimization current_options; | |
5063 | static const char *os_argv[] = { NULL, "-O3", NULL }; | |
5064 | ||
5065 | cl_optimization_save (¤t_options); | |
5066 | decode_options (2, os_argv); | |
5067 | optimization_hot_node = build_optimization_node (); | |
5068 | cl_optimization_restore (¤t_options); | |
5069 | } | |
5070 | ||
5071 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) | |
5072 | = optimization_hot_node; | |
5073 | } | |
a50eecc9 | 5074 | /* Most of the rest of the hot processing is done later with |
5075 | lookup_attribute. */ | |
46f8e3b0 | 5076 | } |
5de92639 | 5077 | } |
5078 | else | |
5079 | { | |
5080 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5081 | *no_add_attrs = true; | |
5082 | } | |
5083 | ||
5084 | return NULL_TREE; | |
5085 | } | |
5086 | /* Handle a "cold" and attribute; arguments as in | |
5087 | struct attribute_spec.handler. */ | |
5088 | ||
5089 | static tree | |
5090 | handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
5091 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
5092 | { | |
5093 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5094 | { | |
5095 | if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL) | |
5096 | { | |
5097 | warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", | |
5098 | name, "hot"); | |
5099 | *no_add_attrs = true; | |
5100 | } | |
46f8e3b0 | 5101 | else |
5102 | { | |
5103 | tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node); | |
5104 | ||
5105 | /* If we are optimizing, but not optimizing for space, turn on -Os | |
5106 | optimizations just for this one function. */ | |
5107 | if (optimize && !optimize_size | |
a50eecc9 | 5108 | && targetm.target_option.cold_attribute_sets_optimization |
46f8e3b0 | 5109 | && (!old_opts || old_opts == optimization_default_node)) |
5110 | { | |
5111 | /* Create the cold optimization node if needed. */ | |
5112 | if (!optimization_cold_node) | |
5113 | { | |
5114 | struct cl_optimization current_options; | |
5115 | static const char *os_argv[] = { NULL, "-Os", NULL }; | |
5116 | ||
5117 | cl_optimization_save (¤t_options); | |
5118 | decode_options (2, os_argv); | |
5119 | optimization_cold_node = build_optimization_node (); | |
5120 | cl_optimization_restore (¤t_options); | |
5121 | } | |
5122 | ||
5123 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) | |
5124 | = optimization_cold_node; | |
5125 | } | |
a50eecc9 | 5126 | /* Most of the rest of the cold processing is done later with |
5127 | lookup_attribute. */ | |
46f8e3b0 | 5128 | } |
5de92639 | 5129 | } |
5130 | else | |
5131 | { | |
5132 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5133 | *no_add_attrs = true; | |
5134 | } | |
5135 | ||
5136 | return NULL_TREE; | |
5137 | } | |
5138 | ||
f8e93a2e | 5139 | /* Handle a "noinline" attribute; arguments as in |
5140 | struct attribute_spec.handler. */ | |
5141 | ||
5142 | static tree | |
1cae46be | 5143 | handle_noinline_attribute (tree *node, tree name, |
9a03a746 | 5144 | tree ARG_UNUSED (args), |
5145 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 5146 | { |
5147 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5148 | DECL_UNINLINABLE (*node) = 1; | |
5149 | else | |
5150 | { | |
9b2d6d13 | 5151 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5152 | *no_add_attrs = true; |
5153 | } | |
5154 | ||
5155 | return NULL_TREE; | |
5156 | } | |
5157 | ||
5158 | /* Handle a "always_inline" attribute; arguments as in | |
5159 | struct attribute_spec.handler. */ | |
5160 | ||
5161 | static tree | |
1cae46be | 5162 | handle_always_inline_attribute (tree *node, tree name, |
9a03a746 | 5163 | tree ARG_UNUSED (args), |
5164 | int ARG_UNUSED (flags), | |
09347743 | 5165 | bool *no_add_attrs) |
f8e93a2e | 5166 | { |
5167 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5168 | { | |
ebb7d626 | 5169 | /* Set the attribute and mark it for disregarding inline |
5170 | limits. */ | |
5171 | DECL_DISREGARD_INLINE_LIMITS (*node) = 1; | |
f8e93a2e | 5172 | } |
5173 | else | |
5174 | { | |
9b2d6d13 | 5175 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5176 | *no_add_attrs = true; |
541e4101 | 5177 | } |
5178 | ||
5179 | return NULL_TREE; | |
5180 | } | |
5181 | ||
5182 | /* Handle a "gnu_inline" attribute; arguments as in | |
5183 | struct attribute_spec.handler. */ | |
5184 | ||
5185 | static tree | |
5186 | handle_gnu_inline_attribute (tree *node, tree name, | |
5187 | tree ARG_UNUSED (args), | |
5188 | int ARG_UNUSED (flags), | |
5189 | bool *no_add_attrs) | |
5190 | { | |
5191 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
5192 | { | |
5193 | /* Do nothing else, just set the attribute. We'll get at | |
5194 | it later with lookup_attribute. */ | |
5195 | } | |
5196 | else | |
5197 | { | |
5198 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5199 | *no_add_attrs = true; | |
f8e93a2e | 5200 | } |
5201 | ||
5202 | return NULL_TREE; | |
5203 | } | |
5204 | ||
1b16fc45 | 5205 | /* Handle an "artificial" attribute; arguments as in |
5206 | struct attribute_spec.handler. */ | |
5207 | ||
5208 | static tree | |
5209 | handle_artificial_attribute (tree *node, tree name, | |
5210 | tree ARG_UNUSED (args), | |
5211 | int ARG_UNUSED (flags), | |
5212 | bool *no_add_attrs) | |
5213 | { | |
5214 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
5215 | { | |
5216 | /* Do nothing else, just set the attribute. We'll get at | |
5217 | it later with lookup_attribute. */ | |
5218 | } | |
5219 | else | |
5220 | { | |
5221 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5222 | *no_add_attrs = true; | |
5223 | } | |
5224 | ||
5225 | return NULL_TREE; | |
5226 | } | |
5227 | ||
0cdd9887 | 5228 | /* Handle a "flatten" attribute; arguments as in |
5229 | struct attribute_spec.handler. */ | |
5230 | ||
5231 | static tree | |
5232 | handle_flatten_attribute (tree *node, tree name, | |
a0c938f0 | 5233 | tree args ATTRIBUTE_UNUSED, |
5234 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
0cdd9887 | 5235 | { |
5236 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5237 | /* Do nothing else, just set the attribute. We'll get at | |
5238 | it later with lookup_attribute. */ | |
5239 | ; | |
5240 | else | |
5241 | { | |
5242 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5243 | *no_add_attrs = true; | |
5244 | } | |
5245 | ||
5246 | return NULL_TREE; | |
5247 | } | |
5248 | ||
10fc867f | 5249 | /* Handle a "warning" or "error" attribute; arguments as in |
5250 | struct attribute_spec.handler. */ | |
5251 | ||
5252 | static tree | |
5253 | handle_error_attribute (tree *node, tree name, tree args, | |
5254 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
5255 | { | |
5256 | if (TREE_CODE (*node) == FUNCTION_DECL | |
5257 | || TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
5258 | /* Do nothing else, just set the attribute. We'll get at | |
5259 | it later with lookup_attribute. */ | |
5260 | ; | |
5261 | else | |
5262 | { | |
5263 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5264 | *no_add_attrs = true; | |
5265 | } | |
5266 | ||
5267 | return NULL_TREE; | |
5268 | } | |
0cdd9887 | 5269 | |
f8e93a2e | 5270 | /* Handle a "used" attribute; arguments as in |
5271 | struct attribute_spec.handler. */ | |
5272 | ||
5273 | static tree | |
9a03a746 | 5274 | handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args), |
5275 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 5276 | { |
d0a31bd8 | 5277 | tree node = *pnode; |
5278 | ||
5279 | if (TREE_CODE (node) == FUNCTION_DECL | |
5280 | || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))) | |
f54ed8bc | 5281 | { |
f54ed8bc | 5282 | TREE_USED (node) = 1; |
9423c9b7 | 5283 | DECL_PRESERVE_P (node) = 1; |
f54ed8bc | 5284 | } |
f8e93a2e | 5285 | else |
5286 | { | |
9b2d6d13 | 5287 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5288 | *no_add_attrs = true; |
5289 | } | |
5290 | ||
5291 | return NULL_TREE; | |
5292 | } | |
5293 | ||
5294 | /* Handle a "unused" attribute; arguments as in | |
5295 | struct attribute_spec.handler. */ | |
5296 | ||
5297 | static tree | |
9a03a746 | 5298 | handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
5299 | int flags, bool *no_add_attrs) | |
f8e93a2e | 5300 | { |
5301 | if (DECL_P (*node)) | |
5302 | { | |
5303 | tree decl = *node; | |
5304 | ||
5305 | if (TREE_CODE (decl) == PARM_DECL | |
5306 | || TREE_CODE (decl) == VAR_DECL | |
5307 | || TREE_CODE (decl) == FUNCTION_DECL | |
5308 | || TREE_CODE (decl) == LABEL_DECL | |
5309 | || TREE_CODE (decl) == TYPE_DECL) | |
5310 | TREE_USED (decl) = 1; | |
5311 | else | |
5312 | { | |
9b2d6d13 | 5313 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5314 | *no_add_attrs = true; |
5315 | } | |
5316 | } | |
5317 | else | |
5318 | { | |
5319 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 5320 | *node = build_variant_type_copy (*node); |
f8e93a2e | 5321 | TREE_USED (*node) = 1; |
5322 | } | |
5323 | ||
5324 | return NULL_TREE; | |
5325 | } | |
5326 | ||
62eec3b4 | 5327 | /* Handle a "externally_visible" attribute; arguments as in |
5328 | struct attribute_spec.handler. */ | |
5329 | ||
5330 | static tree | |
5331 | handle_externally_visible_attribute (tree *pnode, tree name, | |
5332 | tree ARG_UNUSED (args), | |
5333 | int ARG_UNUSED (flags), | |
5334 | bool *no_add_attrs) | |
5335 | { | |
5336 | tree node = *pnode; | |
5337 | ||
ba12ea31 | 5338 | if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL) |
62eec3b4 | 5339 | { |
ba12ea31 | 5340 | if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL |
5341 | && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node)) | |
5342 | { | |
5343 | warning (OPT_Wattributes, | |
5344 | "%qE attribute have effect only on public objects", name); | |
5345 | *no_add_attrs = true; | |
5346 | } | |
62eec3b4 | 5347 | } |
62eec3b4 | 5348 | else |
5349 | { | |
5350 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5351 | *no_add_attrs = true; | |
5352 | } | |
5353 | ||
5354 | return NULL_TREE; | |
5355 | } | |
5356 | ||
f8e93a2e | 5357 | /* Handle a "const" attribute; arguments as in |
5358 | struct attribute_spec.handler. */ | |
5359 | ||
5360 | static tree | |
9a03a746 | 5361 | handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
5362 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 5363 | { |
5364 | tree type = TREE_TYPE (*node); | |
5365 | ||
5366 | /* See FIXME comment on noreturn in c_common_attribute_table. */ | |
5367 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5368 | TREE_READONLY (*node) = 1; | |
5369 | else if (TREE_CODE (type) == POINTER_TYPE | |
5370 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
5371 | TREE_TYPE (*node) | |
5372 | = build_pointer_type | |
5373 | (build_type_variant (TREE_TYPE (type), 1, | |
5374 | TREE_THIS_VOLATILE (TREE_TYPE (type)))); | |
5375 | else | |
5376 | { | |
9b2d6d13 | 5377 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5378 | *no_add_attrs = true; |
5379 | } | |
5380 | ||
5381 | return NULL_TREE; | |
5382 | } | |
5383 | ||
5384 | /* Handle a "transparent_union" attribute; arguments as in | |
5385 | struct attribute_spec.handler. */ | |
5386 | ||
5387 | static tree | |
1cae46be | 5388 | handle_transparent_union_attribute (tree *node, tree name, |
9a03a746 | 5389 | tree ARG_UNUSED (args), int flags, |
09347743 | 5390 | bool *no_add_attrs) |
f8e93a2e | 5391 | { |
881eb642 | 5392 | tree type; |
03908818 | 5393 | |
5394 | *no_add_attrs = true; | |
f8e93a2e | 5395 | |
881eb642 | 5396 | if (TREE_CODE (*node) == TYPE_DECL) |
5397 | node = &TREE_TYPE (*node); | |
5398 | type = *node; | |
f8e93a2e | 5399 | |
03908818 | 5400 | if (TREE_CODE (type) == UNION_TYPE) |
f8e93a2e | 5401 | { |
03908818 | 5402 | /* When IN_PLACE is set, leave the check for FIELDS and MODE to |
5403 | the code in finish_struct. */ | |
f8e93a2e | 5404 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
03908818 | 5405 | { |
5406 | if (TYPE_FIELDS (type) == NULL_TREE | |
5407 | || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type))) | |
5408 | goto ignored; | |
5409 | ||
5410 | /* A type variant isn't good enough, since we don't a cast | |
5411 | to such a type removed as a no-op. */ | |
5412 | *node = type = build_duplicate_type (type); | |
5413 | } | |
5414 | ||
5415 | TYPE_TRANSPARENT_UNION (type) = 1; | |
5416 | return NULL_TREE; | |
f8e93a2e | 5417 | } |
5418 | ||
03908818 | 5419 | ignored: |
5420 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 5421 | return NULL_TREE; |
5422 | } | |
5423 | ||
9af7fd5b | 5424 | /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to |
5425 | get the requested priority for a constructor or destructor, | |
5426 | possibly issuing diagnostics for invalid or reserved | |
5427 | priorities. */ | |
5428 | ||
5429 | static priority_type | |
5430 | get_priority (tree args, bool is_destructor) | |
5431 | { | |
5432 | HOST_WIDE_INT pri; | |
6c181a06 | 5433 | tree arg; |
9af7fd5b | 5434 | |
5435 | if (!args) | |
5436 | return DEFAULT_INIT_PRIORITY; | |
6c181a06 | 5437 | |
28fbc04f | 5438 | if (!SUPPORTS_INIT_PRIORITY) |
5439 | { | |
5440 | if (is_destructor) | |
5441 | error ("destructor priorities are not supported"); | |
5442 | else | |
5443 | error ("constructor priorities are not supported"); | |
5444 | return DEFAULT_INIT_PRIORITY; | |
5445 | } | |
5446 | ||
6c181a06 | 5447 | arg = TREE_VALUE (args); |
5448 | if (!host_integerp (arg, /*pos=*/0) | |
5449 | || !INTEGRAL_TYPE_P (TREE_TYPE (arg))) | |
9af7fd5b | 5450 | goto invalid; |
5451 | ||
5452 | pri = tree_low_cst (TREE_VALUE (args), /*pos=*/0); | |
5453 | if (pri < 0 || pri > MAX_INIT_PRIORITY) | |
5454 | goto invalid; | |
5455 | ||
5456 | if (pri <= MAX_RESERVED_INIT_PRIORITY) | |
5457 | { | |
5458 | if (is_destructor) | |
5459 | warning (0, | |
5460 | "destructor priorities from 0 to %d are reserved " | |
5461 | "for the implementation", | |
5462 | MAX_RESERVED_INIT_PRIORITY); | |
5463 | else | |
5464 | warning (0, | |
5465 | "constructor priorities from 0 to %d are reserved " | |
5466 | "for the implementation", | |
5467 | MAX_RESERVED_INIT_PRIORITY); | |
5468 | } | |
5469 | return pri; | |
5470 | ||
5471 | invalid: | |
5472 | if (is_destructor) | |
5473 | error ("destructor priorities must be integers from 0 to %d inclusive", | |
5474 | MAX_INIT_PRIORITY); | |
5475 | else | |
5476 | error ("constructor priorities must be integers from 0 to %d inclusive", | |
5477 | MAX_INIT_PRIORITY); | |
5478 | return DEFAULT_INIT_PRIORITY; | |
5479 | } | |
5480 | ||
f8e93a2e | 5481 | /* Handle a "constructor" attribute; arguments as in |
5482 | struct attribute_spec.handler. */ | |
5483 | ||
5484 | static tree | |
9af7fd5b | 5485 | handle_constructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 5486 | int ARG_UNUSED (flags), |
09347743 | 5487 | bool *no_add_attrs) |
f8e93a2e | 5488 | { |
5489 | tree decl = *node; | |
5490 | tree type = TREE_TYPE (decl); | |
5491 | ||
5492 | if (TREE_CODE (decl) == FUNCTION_DECL | |
5493 | && TREE_CODE (type) == FUNCTION_TYPE | |
5494 | && decl_function_context (decl) == 0) | |
5495 | { | |
9af7fd5b | 5496 | priority_type priority; |
f8e93a2e | 5497 | DECL_STATIC_CONSTRUCTOR (decl) = 1; |
9af7fd5b | 5498 | priority = get_priority (args, /*is_destructor=*/false); |
5499 | SET_DECL_INIT_PRIORITY (decl, priority); | |
f8e93a2e | 5500 | TREE_USED (decl) = 1; |
5501 | } | |
5502 | else | |
5503 | { | |
9b2d6d13 | 5504 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5505 | *no_add_attrs = true; |
5506 | } | |
5507 | ||
5508 | return NULL_TREE; | |
5509 | } | |
5510 | ||
5511 | /* Handle a "destructor" attribute; arguments as in | |
5512 | struct attribute_spec.handler. */ | |
5513 | ||
5514 | static tree | |
9af7fd5b | 5515 | handle_destructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 5516 | int ARG_UNUSED (flags), |
09347743 | 5517 | bool *no_add_attrs) |
f8e93a2e | 5518 | { |
5519 | tree decl = *node; | |
5520 | tree type = TREE_TYPE (decl); | |
5521 | ||
5522 | if (TREE_CODE (decl) == FUNCTION_DECL | |
5523 | && TREE_CODE (type) == FUNCTION_TYPE | |
5524 | && decl_function_context (decl) == 0) | |
5525 | { | |
9af7fd5b | 5526 | priority_type priority; |
f8e93a2e | 5527 | DECL_STATIC_DESTRUCTOR (decl) = 1; |
9af7fd5b | 5528 | priority = get_priority (args, /*is_destructor=*/true); |
5529 | SET_DECL_FINI_PRIORITY (decl, priority); | |
f8e93a2e | 5530 | TREE_USED (decl) = 1; |
5531 | } | |
5532 | else | |
5533 | { | |
9b2d6d13 | 5534 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5535 | *no_add_attrs = true; |
5536 | } | |
5537 | ||
5538 | return NULL_TREE; | |
5539 | } | |
5540 | ||
5541 | /* Handle a "mode" attribute; arguments as in | |
5542 | struct attribute_spec.handler. */ | |
5543 | ||
5544 | static tree | |
9a03a746 | 5545 | handle_mode_attribute (tree *node, tree name, tree args, |
5546 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 5547 | { |
5548 | tree type = *node; | |
5549 | ||
5550 | *no_add_attrs = true; | |
5551 | ||
5552 | if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE) | |
9b2d6d13 | 5553 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5554 | else |
5555 | { | |
5556 | int j; | |
5557 | const char *p = IDENTIFIER_POINTER (TREE_VALUE (args)); | |
5558 | int len = strlen (p); | |
5559 | enum machine_mode mode = VOIDmode; | |
5560 | tree typefm; | |
b2aef146 | 5561 | bool valid_mode; |
f8e93a2e | 5562 | |
5563 | if (len > 4 && p[0] == '_' && p[1] == '_' | |
5564 | && p[len - 1] == '_' && p[len - 2] == '_') | |
5565 | { | |
4fd61bc6 | 5566 | char *newp = (char *) alloca (len - 1); |
f8e93a2e | 5567 | |
5568 | strcpy (newp, &p[2]); | |
5569 | newp[len - 4] = '\0'; | |
5570 | p = newp; | |
5571 | } | |
5572 | ||
5573 | /* Change this type to have a type with the specified mode. | |
5574 | First check for the special modes. */ | |
84166705 | 5575 | if (!strcmp (p, "byte")) |
f8e93a2e | 5576 | mode = byte_mode; |
5577 | else if (!strcmp (p, "word")) | |
5578 | mode = word_mode; | |
84166705 | 5579 | else if (!strcmp (p, "pointer")) |
f8e93a2e | 5580 | mode = ptr_mode; |
0ef89dfd | 5581 | else if (!strcmp (p, "libgcc_cmp_return")) |
5582 | mode = targetm.libgcc_cmp_return_mode (); | |
5583 | else if (!strcmp (p, "libgcc_shift_count")) | |
5584 | mode = targetm.libgcc_shift_count_mode (); | |
1bd43494 | 5585 | else if (!strcmp (p, "unwind_word")) |
5586 | mode = targetm.unwind_word_mode (); | |
f8e93a2e | 5587 | else |
5588 | for (j = 0; j < NUM_MACHINE_MODES; j++) | |
5589 | if (!strcmp (p, GET_MODE_NAME (j))) | |
743a6f47 | 5590 | { |
5591 | mode = (enum machine_mode) j; | |
5592 | break; | |
5593 | } | |
f8e93a2e | 5594 | |
5595 | if (mode == VOIDmode) | |
4917c376 | 5596 | { |
1e5fcbe2 | 5597 | error ("unknown machine mode %qs", p); |
4917c376 | 5598 | return NULL_TREE; |
5599 | } | |
5600 | ||
b2aef146 | 5601 | valid_mode = false; |
5602 | switch (GET_MODE_CLASS (mode)) | |
4917c376 | 5603 | { |
b2aef146 | 5604 | case MODE_INT: |
5605 | case MODE_PARTIAL_INT: | |
5606 | case MODE_FLOAT: | |
c4503c0a | 5607 | case MODE_DECIMAL_FLOAT: |
9421ebb9 | 5608 | case MODE_FRACT: |
5609 | case MODE_UFRACT: | |
5610 | case MODE_ACCUM: | |
5611 | case MODE_UACCUM: | |
b2aef146 | 5612 | valid_mode = targetm.scalar_mode_supported_p (mode); |
5613 | break; | |
5614 | ||
5615 | case MODE_COMPLEX_INT: | |
5616 | case MODE_COMPLEX_FLOAT: | |
5617 | valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode)); | |
5618 | break; | |
5619 | ||
5620 | case MODE_VECTOR_INT: | |
5621 | case MODE_VECTOR_FLOAT: | |
9421ebb9 | 5622 | case MODE_VECTOR_FRACT: |
5623 | case MODE_VECTOR_UFRACT: | |
5624 | case MODE_VECTOR_ACCUM: | |
5625 | case MODE_VECTOR_UACCUM: | |
9b2d6d13 | 5626 | warning (OPT_Wattributes, "specifying vector types with " |
5627 | "__attribute__ ((mode)) is deprecated"); | |
5628 | warning (OPT_Wattributes, | |
5629 | "use __attribute__ ((vector_size)) instead"); | |
b2aef146 | 5630 | valid_mode = vector_mode_valid_p (mode); |
5631 | break; | |
4917c376 | 5632 | |
b2aef146 | 5633 | default: |
5634 | break; | |
5635 | } | |
5636 | if (!valid_mode) | |
5637 | { | |
1e5fcbe2 | 5638 | error ("unable to emulate %qs", p); |
b2aef146 | 5639 | return NULL_TREE; |
5640 | } | |
4917c376 | 5641 | |
b2aef146 | 5642 | if (POINTER_TYPE_P (type)) |
ead34f59 | 5643 | { |
b2aef146 | 5644 | tree (*fn)(tree, enum machine_mode, bool); |
5645 | ||
5646 | if (!targetm.valid_pointer_mode (mode)) | |
ead34f59 | 5647 | { |
1e5fcbe2 | 5648 | error ("invalid pointer mode %qs", p); |
ead34f59 | 5649 | return NULL_TREE; |
5650 | } | |
5651 | ||
a0c938f0 | 5652 | if (TREE_CODE (type) == POINTER_TYPE) |
b2aef146 | 5653 | fn = build_pointer_type_for_mode; |
805e22b2 | 5654 | else |
b2aef146 | 5655 | fn = build_reference_type_for_mode; |
5656 | typefm = fn (TREE_TYPE (type), mode, false); | |
ead34f59 | 5657 | } |
b2aef146 | 5658 | else |
9421ebb9 | 5659 | { |
5660 | /* For fixed-point modes, we need to test if the signness of type | |
5661 | and the machine mode are consistent. */ | |
5662 | if (ALL_FIXED_POINT_MODE_P (mode) | |
5663 | && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode)) | |
5664 | { | |
5665 | error ("signness of type and machine mode %qs don't match", p); | |
5666 | return NULL_TREE; | |
5667 | } | |
5668 | /* For fixed-point modes, we need to pass saturating info. */ | |
5669 | typefm = lang_hooks.types.type_for_mode (mode, | |
5670 | ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type) | |
5671 | : TYPE_UNSIGNED (type)); | |
5672 | } | |
3a648ab9 | 5673 | |
b2aef146 | 5674 | if (typefm == NULL_TREE) |
5675 | { | |
743a6f47 | 5676 | error ("no data type for mode %qs", p); |
b2aef146 | 5677 | return NULL_TREE; |
5678 | } | |
3a648ab9 | 5679 | else if (TREE_CODE (type) == ENUMERAL_TYPE) |
5680 | { | |
5681 | /* For enumeral types, copy the precision from the integer | |
5682 | type returned above. If not an INTEGER_TYPE, we can't use | |
5683 | this mode for this type. */ | |
5684 | if (TREE_CODE (typefm) != INTEGER_TYPE) | |
5685 | { | |
743a6f47 | 5686 | error ("cannot use mode %qs for enumeral types", p); |
3a648ab9 | 5687 | return NULL_TREE; |
5688 | } | |
5689 | ||
10080eac | 5690 | if (flags & ATTR_FLAG_TYPE_IN_PLACE) |
5691 | { | |
5692 | TYPE_PRECISION (type) = TYPE_PRECISION (typefm); | |
5693 | typefm = type; | |
5694 | } | |
5695 | else | |
5696 | { | |
5697 | /* We cannot build a type variant, as there's code that assumes | |
5698 | that TYPE_MAIN_VARIANT has the same mode. This includes the | |
5699 | debug generators. Instead, create a subrange type. This | |
5700 | results in all of the enumeral values being emitted only once | |
5701 | in the original, and the subtype gets them by reference. */ | |
5702 | if (TYPE_UNSIGNED (type)) | |
5703 | typefm = make_unsigned_type (TYPE_PRECISION (typefm)); | |
5704 | else | |
5705 | typefm = make_signed_type (TYPE_PRECISION (typefm)); | |
5706 | TREE_TYPE (typefm) = type; | |
5707 | } | |
3a648ab9 | 5708 | } |
4bf450a1 | 5709 | else if (VECTOR_MODE_P (mode) |
5710 | ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm)) | |
5711 | : TREE_CODE (type) != TREE_CODE (typefm)) | |
743a6f47 | 5712 | { |
5713 | error ("mode %qs applied to inappropriate type", p); | |
5714 | return NULL_TREE; | |
5715 | } | |
5716 | ||
b2aef146 | 5717 | *node = typefm; |
f8e93a2e | 5718 | } |
5719 | ||
5720 | return NULL_TREE; | |
5721 | } | |
5722 | ||
5723 | /* Handle a "section" attribute; arguments as in | |
5724 | struct attribute_spec.handler. */ | |
5725 | ||
5726 | static tree | |
9a03a746 | 5727 | handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
5728 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 5729 | { |
5730 | tree decl = *node; | |
5731 | ||
5732 | if (targetm.have_named_sections) | |
5733 | { | |
065efcb1 | 5734 | user_defined_section_attribute = true; |
5735 | ||
f8e93a2e | 5736 | if ((TREE_CODE (decl) == FUNCTION_DECL |
5737 | || TREE_CODE (decl) == VAR_DECL) | |
5738 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
5739 | { | |
5740 | if (TREE_CODE (decl) == VAR_DECL | |
5741 | && current_function_decl != NULL_TREE | |
84166705 | 5742 | && !TREE_STATIC (decl)) |
f8e93a2e | 5743 | { |
9bc3739f | 5744 | error ("%Jsection attribute cannot be specified for " |
4ee9c684 | 5745 | "local variables", decl); |
f8e93a2e | 5746 | *no_add_attrs = true; |
5747 | } | |
5748 | ||
5749 | /* The decl may have already been given a section attribute | |
5750 | from a previous declaration. Ensure they match. */ | |
5751 | else if (DECL_SECTION_NAME (decl) != NULL_TREE | |
5752 | && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), | |
5753 | TREE_STRING_POINTER (TREE_VALUE (args))) != 0) | |
5754 | { | |
3cf8b391 | 5755 | error ("section of %q+D conflicts with previous declaration", |
5756 | *node); | |
f8e93a2e | 5757 | *no_add_attrs = true; |
5758 | } | |
38475469 | 5759 | else if (TREE_CODE (decl) == VAR_DECL |
5760 | && !targetm.have_tls && targetm.emutls.tmpl_section | |
5761 | && DECL_THREAD_LOCAL_P (decl)) | |
5762 | { | |
5763 | error ("section of %q+D cannot be overridden", *node); | |
5764 | *no_add_attrs = true; | |
5765 | } | |
f8e93a2e | 5766 | else |
5767 | DECL_SECTION_NAME (decl) = TREE_VALUE (args); | |
5768 | } | |
5769 | else | |
5770 | { | |
3cf8b391 | 5771 | error ("section attribute not allowed for %q+D", *node); |
f8e93a2e | 5772 | *no_add_attrs = true; |
5773 | } | |
5774 | } | |
5775 | else | |
5776 | { | |
9bc3739f | 5777 | error ("%Jsection attributes are not supported for this target", *node); |
f8e93a2e | 5778 | *no_add_attrs = true; |
5779 | } | |
5780 | ||
5781 | return NULL_TREE; | |
5782 | } | |
5783 | ||
5784 | /* Handle a "aligned" attribute; arguments as in | |
5785 | struct attribute_spec.handler. */ | |
5786 | ||
5787 | static tree | |
9a03a746 | 5788 | handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
09347743 | 5789 | int flags, bool *no_add_attrs) |
f8e93a2e | 5790 | { |
5791 | tree decl = NULL_TREE; | |
5792 | tree *type = NULL; | |
5793 | int is_type = 0; | |
5794 | tree align_expr = (args ? TREE_VALUE (args) | |
5795 | : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT)); | |
5796 | int i; | |
5797 | ||
5798 | if (DECL_P (*node)) | |
5799 | { | |
5800 | decl = *node; | |
5801 | type = &TREE_TYPE (decl); | |
5802 | is_type = TREE_CODE (*node) == TYPE_DECL; | |
5803 | } | |
5804 | else if (TYPE_P (*node)) | |
5805 | type = node, is_type = 1; | |
5806 | ||
f8e93a2e | 5807 | if (TREE_CODE (align_expr) != INTEGER_CST) |
5808 | { | |
5809 | error ("requested alignment is not a constant"); | |
5810 | *no_add_attrs = true; | |
5811 | } | |
5812 | else if ((i = tree_log2 (align_expr)) == -1) | |
5813 | { | |
5814 | error ("requested alignment is not a power of 2"); | |
5815 | *no_add_attrs = true; | |
5816 | } | |
5817 | else if (i > HOST_BITS_PER_INT - 2) | |
5818 | { | |
5819 | error ("requested alignment is too large"); | |
5820 | *no_add_attrs = true; | |
5821 | } | |
5822 | else if (is_type) | |
5823 | { | |
5824 | /* If we have a TYPE_DECL, then copy the type, so that we | |
5825 | don't accidentally modify a builtin type. See pushdecl. */ | |
5826 | if (decl && TREE_TYPE (decl) != error_mark_node | |
5827 | && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) | |
5828 | { | |
5829 | tree tt = TREE_TYPE (decl); | |
e086912e | 5830 | *type = build_variant_type_copy (*type); |
f8e93a2e | 5831 | DECL_ORIGINAL_TYPE (decl) = tt; |
5832 | TYPE_NAME (*type) = decl; | |
5833 | TREE_USED (*type) = TREE_USED (decl); | |
5834 | TREE_TYPE (decl) = *type; | |
5835 | } | |
5836 | else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 5837 | *type = build_variant_type_copy (*type); |
f8e93a2e | 5838 | |
5839 | TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT; | |
5840 | TYPE_USER_ALIGN (*type) = 1; | |
5841 | } | |
097b5c8b | 5842 | else if (! VAR_OR_FUNCTION_DECL_P (decl) |
f8e93a2e | 5843 | && TREE_CODE (decl) != FIELD_DECL) |
5844 | { | |
3cf8b391 | 5845 | error ("alignment may not be specified for %q+D", decl); |
f8e93a2e | 5846 | *no_add_attrs = true; |
5847 | } | |
097b5c8b | 5848 | else if (TREE_CODE (decl) == FUNCTION_DECL |
5849 | && DECL_ALIGN (decl) > (1 << i) * BITS_PER_UNIT) | |
5850 | { | |
5851 | if (DECL_USER_ALIGN (decl)) | |
5852 | error ("alignment for %q+D was previously specified as %d " | |
5853 | "and may not be decreased", decl, | |
5854 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
5855 | else | |
5856 | error ("alignment for %q+D must be at least %d", decl, | |
5857 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
5858 | *no_add_attrs = true; | |
5859 | } | |
f8e93a2e | 5860 | else |
5861 | { | |
5862 | DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT; | |
5863 | DECL_USER_ALIGN (decl) = 1; | |
5864 | } | |
5865 | ||
5866 | return NULL_TREE; | |
5867 | } | |
5868 | ||
5869 | /* Handle a "weak" attribute; arguments as in | |
5870 | struct attribute_spec.handler. */ | |
5871 | ||
5872 | static tree | |
f948b309 | 5873 | handle_weak_attribute (tree *node, tree name, |
9a03a746 | 5874 | tree ARG_UNUSED (args), |
5875 | int ARG_UNUSED (flags), | |
5876 | bool * ARG_UNUSED (no_add_attrs)) | |
f8e93a2e | 5877 | { |
f948b309 | 5878 | if (TREE_CODE (*node) == FUNCTION_DECL |
059a60f3 | 5879 | && DECL_DECLARED_INLINE_P (*node)) |
5880 | { | |
5881 | error ("inline function %q+D cannot be declared weak", *node); | |
5882 | *no_add_attrs = true; | |
5883 | } | |
5884 | else if (TREE_CODE (*node) == FUNCTION_DECL | |
5885 | || TREE_CODE (*node) == VAR_DECL) | |
f948b309 | 5886 | declare_weak (*node); |
5887 | else | |
5888 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 5889 | |
5890 | return NULL_TREE; | |
5891 | } | |
5892 | ||
5893 | /* Handle an "alias" attribute; arguments as in | |
5894 | struct attribute_spec.handler. */ | |
5895 | ||
5896 | static tree | |
1cae46be | 5897 | handle_alias_attribute (tree *node, tree name, tree args, |
9a03a746 | 5898 | int ARG_UNUSED (flags), bool *no_add_attrs) |
f8e93a2e | 5899 | { |
5900 | tree decl = *node; | |
5901 | ||
5902 | if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) | |
0a3ecdc1 | 5903 | || (TREE_CODE (decl) != FUNCTION_DECL |
5904 | && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) | |
5905 | /* A static variable declaration is always a tentative definition, | |
5906 | but the alias is a non-tentative definition which overrides. */ | |
5907 | || (TREE_CODE (decl) != FUNCTION_DECL | |
5908 | && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl))) | |
f8e93a2e | 5909 | { |
3cf8b391 | 5910 | error ("%q+D defined both normally and as an alias", decl); |
f8e93a2e | 5911 | *no_add_attrs = true; |
5912 | } | |
8c42f0d9 | 5913 | |
5914 | /* Note that the very first time we process a nested declaration, | |
5915 | decl_function_context will not be set. Indeed, *would* never | |
5916 | be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that | |
5917 | we do below. After such frobbery, pushdecl would set the context. | |
5918 | In any case, this is never what we want. */ | |
5919 | else if (decl_function_context (decl) == 0 && current_function_decl == NULL) | |
f8e93a2e | 5920 | { |
5921 | tree id; | |
5922 | ||
5923 | id = TREE_VALUE (args); | |
5924 | if (TREE_CODE (id) != STRING_CST) | |
5925 | { | |
07e3a3d2 | 5926 | error ("alias argument not a string"); |
f8e93a2e | 5927 | *no_add_attrs = true; |
5928 | return NULL_TREE; | |
5929 | } | |
5930 | id = get_identifier (TREE_STRING_POINTER (id)); | |
5931 | /* This counts as a use of the object pointed to. */ | |
5932 | TREE_USED (id) = 1; | |
5933 | ||
5934 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
5935 | DECL_INITIAL (decl) = error_mark_node; | |
5936 | else | |
4ee9c684 | 5937 | { |
f4a30bd7 | 5938 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) |
5939 | DECL_EXTERNAL (decl) = 1; | |
5940 | else | |
5941 | DECL_EXTERNAL (decl) = 0; | |
4ee9c684 | 5942 | TREE_STATIC (decl) = 1; |
5943 | } | |
f8e93a2e | 5944 | } |
5945 | else | |
5946 | { | |
9b2d6d13 | 5947 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5948 | *no_add_attrs = true; |
5949 | } | |
5950 | ||
5951 | return NULL_TREE; | |
5952 | } | |
5953 | ||
f4a30bd7 | 5954 | /* Handle a "weakref" attribute; arguments as in struct |
5955 | attribute_spec.handler. */ | |
5956 | ||
5957 | static tree | |
5958 | handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
5959 | int flags, bool *no_add_attrs) | |
5960 | { | |
5961 | tree attr = NULL_TREE; | |
5962 | ||
83852912 | 5963 | /* We must ignore the attribute when it is associated with |
5964 | local-scoped decls, since attribute alias is ignored and many | |
5965 | such symbols do not even have a DECL_WEAK field. */ | |
5966 | if (decl_function_context (*node) || current_function_decl) | |
5967 | { | |
5968 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5969 | *no_add_attrs = true; | |
5970 | return NULL_TREE; | |
5971 | } | |
5972 | ||
f4a30bd7 | 5973 | /* The idea here is that `weakref("name")' mutates into `weakref, |
5974 | alias("name")', and weakref without arguments, in turn, | |
5975 | implicitly adds weak. */ | |
5976 | ||
5977 | if (args) | |
5978 | { | |
5979 | attr = tree_cons (get_identifier ("alias"), args, attr); | |
5980 | attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr); | |
5981 | ||
5982 | *no_add_attrs = true; | |
0a3ecdc1 | 5983 | |
5984 | decl_attributes (node, attr, flags); | |
f4a30bd7 | 5985 | } |
5986 | else | |
5987 | { | |
5988 | if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node))) | |
5989 | error ("%Jweakref attribute must appear before alias attribute", | |
5990 | *node); | |
5991 | ||
0a3ecdc1 | 5992 | /* Can't call declare_weak because it wants this to be TREE_PUBLIC, |
5993 | and that isn't supported; and because it wants to add it to | |
5994 | the list of weak decls, which isn't helpful. */ | |
5995 | DECL_WEAK (*node) = 1; | |
f4a30bd7 | 5996 | } |
5997 | ||
f4a30bd7 | 5998 | return NULL_TREE; |
5999 | } | |
6000 | ||
f8e93a2e | 6001 | /* Handle an "visibility" attribute; arguments as in |
6002 | struct attribute_spec.handler. */ | |
6003 | ||
6004 | static tree | |
1cae46be | 6005 | handle_visibility_attribute (tree *node, tree name, tree args, |
9a03a746 | 6006 | int ARG_UNUSED (flags), |
4a2849cb | 6007 | bool *ARG_UNUSED (no_add_attrs)) |
f8e93a2e | 6008 | { |
6009 | tree decl = *node; | |
9c40570a | 6010 | tree id = TREE_VALUE (args); |
4a2849cb | 6011 | enum symbol_visibility vis; |
f8e93a2e | 6012 | |
b212f378 | 6013 | if (TYPE_P (*node)) |
6014 | { | |
4a2849cb | 6015 | if (TREE_CODE (*node) == ENUMERAL_TYPE) |
6016 | /* OK */; | |
6017 | else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE) | |
6018 | { | |
6019 | warning (OPT_Wattributes, "%qE attribute ignored on non-class types", | |
6020 | name); | |
6021 | return NULL_TREE; | |
6022 | } | |
6023 | else if (TYPE_FIELDS (*node)) | |
6024 | { | |
6025 | error ("%qE attribute ignored because %qT is already defined", | |
6026 | name, *node); | |
6027 | return NULL_TREE; | |
6028 | } | |
b212f378 | 6029 | } |
84166705 | 6030 | else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl)) |
f8e93a2e | 6031 | { |
9b2d6d13 | 6032 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
9c40570a | 6033 | return NULL_TREE; |
f8e93a2e | 6034 | } |
f8e93a2e | 6035 | |
9c40570a | 6036 | if (TREE_CODE (id) != STRING_CST) |
6037 | { | |
07e3a3d2 | 6038 | error ("visibility argument not a string"); |
9c40570a | 6039 | return NULL_TREE; |
f8e93a2e | 6040 | } |
b27ac6b5 | 6041 | |
b212f378 | 6042 | /* If this is a type, set the visibility on the type decl. */ |
6043 | if (TYPE_P (decl)) | |
6044 | { | |
6045 | decl = TYPE_NAME (decl); | |
84166705 | 6046 | if (!decl) |
a0c938f0 | 6047 | return NULL_TREE; |
e147aab3 | 6048 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
6049 | { | |
9b2d6d13 | 6050 | warning (OPT_Wattributes, "%qE attribute ignored on types", |
e147aab3 | 6051 | name); |
6052 | return NULL_TREE; | |
6053 | } | |
b212f378 | 6054 | } |
f8e93a2e | 6055 | |
9c40570a | 6056 | if (strcmp (TREE_STRING_POINTER (id), "default") == 0) |
4a2849cb | 6057 | vis = VISIBILITY_DEFAULT; |
9c40570a | 6058 | else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0) |
4a2849cb | 6059 | vis = VISIBILITY_INTERNAL; |
9c40570a | 6060 | else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0) |
4a2849cb | 6061 | vis = VISIBILITY_HIDDEN; |
9c40570a | 6062 | else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0) |
4a2849cb | 6063 | vis = VISIBILITY_PROTECTED; |
9c40570a | 6064 | else |
4a2849cb | 6065 | { |
6066 | error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\""); | |
6067 | vis = VISIBILITY_DEFAULT; | |
6068 | } | |
6069 | ||
6070 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
098a01e7 | 6071 | && vis != DECL_VISIBILITY (decl)) |
6072 | { | |
6073 | tree attributes = (TYPE_P (*node) | |
6074 | ? TYPE_ATTRIBUTES (*node) | |
6075 | : DECL_ATTRIBUTES (decl)); | |
6076 | if (lookup_attribute ("visibility", attributes)) | |
6077 | error ("%qD redeclared with different visibility", decl); | |
6078 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
6079 | && lookup_attribute ("dllimport", attributes)) | |
6080 | error ("%qD was declared %qs which implies default visibility", | |
6081 | decl, "dllimport"); | |
6082 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
6083 | && lookup_attribute ("dllexport", attributes)) | |
6084 | error ("%qD was declared %qs which implies default visibility", | |
6085 | decl, "dllexport"); | |
6086 | } | |
4a2849cb | 6087 | |
6088 | DECL_VISIBILITY (decl) = vis; | |
b212f378 | 6089 | DECL_VISIBILITY_SPECIFIED (decl) = 1; |
6090 | ||
4a2849cb | 6091 | /* Go ahead and attach the attribute to the node as well. This is needed |
6092 | so we can determine whether we have VISIBILITY_DEFAULT because the | |
6093 | visibility was not specified, or because it was explicitly overridden | |
6094 | from the containing scope. */ | |
9c40570a | 6095 | |
f8e93a2e | 6096 | return NULL_TREE; |
6097 | } | |
6098 | ||
3aa0c315 | 6099 | /* Determine the ELF symbol visibility for DECL, which is either a |
6100 | variable or a function. It is an error to use this function if a | |
6101 | definition of DECL is not available in this translation unit. | |
6102 | Returns true if the final visibility has been determined by this | |
6103 | function; false if the caller is free to make additional | |
6104 | modifications. */ | |
6105 | ||
6106 | bool | |
6107 | c_determine_visibility (tree decl) | |
6108 | { | |
1d8fc210 | 6109 | gcc_assert (TREE_CODE (decl) == VAR_DECL |
6110 | || TREE_CODE (decl) == FUNCTION_DECL); | |
3aa0c315 | 6111 | |
6112 | /* If the user explicitly specified the visibility with an | |
6113 | attribute, honor that. DECL_VISIBILITY will have been set during | |
6114 | the processing of the attribute. We check for an explicit | |
6115 | attribute, rather than just checking DECL_VISIBILITY_SPECIFIED, | |
6116 | to distinguish the use of an attribute from the use of a "#pragma | |
6117 | GCC visibility push(...)"; in the latter case we still want other | |
6118 | considerations to be able to overrule the #pragma. */ | |
098a01e7 | 6119 | if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)) |
6120 | || (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
6121 | && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) | |
6122 | || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))))) | |
3aa0c315 | 6123 | return true; |
6124 | ||
4a2849cb | 6125 | /* Set default visibility to whatever the user supplied with |
6126 | visibility_specified depending on #pragma GCC visibility. */ | |
6127 | if (!DECL_VISIBILITY_SPECIFIED (decl)) | |
6128 | { | |
6129 | DECL_VISIBILITY (decl) = default_visibility; | |
6130 | DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; | |
6131 | } | |
3aa0c315 | 6132 | return false; |
6133 | } | |
6134 | ||
24dfead4 | 6135 | /* Handle an "tls_model" attribute; arguments as in |
6136 | struct attribute_spec.handler. */ | |
6137 | ||
6138 | static tree | |
1cae46be | 6139 | handle_tls_model_attribute (tree *node, tree name, tree args, |
9a03a746 | 6140 | int ARG_UNUSED (flags), bool *no_add_attrs) |
24dfead4 | 6141 | { |
1b53eb20 | 6142 | tree id; |
24dfead4 | 6143 | tree decl = *node; |
1b53eb20 | 6144 | enum tls_model kind; |
24dfead4 | 6145 | |
1b53eb20 | 6146 | *no_add_attrs = true; |
6147 | ||
6148 | if (!DECL_THREAD_LOCAL_P (decl)) | |
24dfead4 | 6149 | { |
9b2d6d13 | 6150 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
1b53eb20 | 6151 | return NULL_TREE; |
24dfead4 | 6152 | } |
24dfead4 | 6153 | |
1b53eb20 | 6154 | kind = DECL_TLS_MODEL (decl); |
6155 | id = TREE_VALUE (args); | |
6156 | if (TREE_CODE (id) != STRING_CST) | |
6157 | { | |
6158 | error ("tls_model argument not a string"); | |
6159 | return NULL_TREE; | |
24dfead4 | 6160 | } |
6161 | ||
1b53eb20 | 6162 | if (!strcmp (TREE_STRING_POINTER (id), "local-exec")) |
6163 | kind = TLS_MODEL_LOCAL_EXEC; | |
6164 | else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec")) | |
6165 | kind = TLS_MODEL_INITIAL_EXEC; | |
6166 | else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic")) | |
6167 | kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC; | |
6168 | else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic")) | |
6169 | kind = TLS_MODEL_GLOBAL_DYNAMIC; | |
6170 | else | |
6171 | error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\""); | |
6172 | ||
6173 | DECL_TLS_MODEL (decl) = kind; | |
24dfead4 | 6174 | return NULL_TREE; |
6175 | } | |
6176 | ||
f8e93a2e | 6177 | /* Handle a "no_instrument_function" attribute; arguments as in |
6178 | struct attribute_spec.handler. */ | |
6179 | ||
6180 | static tree | |
1cae46be | 6181 | handle_no_instrument_function_attribute (tree *node, tree name, |
9a03a746 | 6182 | tree ARG_UNUSED (args), |
6183 | int ARG_UNUSED (flags), | |
09347743 | 6184 | bool *no_add_attrs) |
f8e93a2e | 6185 | { |
6186 | tree decl = *node; | |
6187 | ||
6188 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
6189 | { | |
b0b1af64 | 6190 | error ("%J%qE attribute applies only to functions", decl, name); |
f8e93a2e | 6191 | *no_add_attrs = true; |
6192 | } | |
6193 | else if (DECL_INITIAL (decl)) | |
6194 | { | |
b0b1af64 | 6195 | error ("%Jcan%'t set %qE attribute after definition", decl, name); |
f8e93a2e | 6196 | *no_add_attrs = true; |
6197 | } | |
6198 | else | |
6199 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
6200 | ||
6201 | return NULL_TREE; | |
6202 | } | |
6203 | ||
6204 | /* Handle a "malloc" attribute; arguments as in | |
6205 | struct attribute_spec.handler. */ | |
6206 | ||
6207 | static tree | |
9a03a746 | 6208 | handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6209 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6210 | { |
a5147fca | 6211 | if (TREE_CODE (*node) == FUNCTION_DECL |
6212 | && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) | |
f8e93a2e | 6213 | DECL_IS_MALLOC (*node) = 1; |
f8e93a2e | 6214 | else |
6215 | { | |
9b2d6d13 | 6216 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6217 | *no_add_attrs = true; |
6218 | } | |
6219 | ||
6220 | return NULL_TREE; | |
6221 | } | |
6222 | ||
4a29c97c | 6223 | /* Handle a "alloc_size" attribute; arguments as in |
6224 | struct attribute_spec.handler. */ | |
6225 | ||
6226 | static tree | |
6227 | handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
6228 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6229 | { | |
2802826e | 6230 | unsigned arg_count = type_num_arguments (*node); |
4a29c97c | 6231 | for (; args; args = TREE_CHAIN (args)) |
6232 | { | |
6233 | tree position = TREE_VALUE (args); | |
6234 | ||
6235 | if (TREE_CODE (position) != INTEGER_CST | |
6236 | || TREE_INT_CST_HIGH (position) | |
6237 | || TREE_INT_CST_LOW (position) < 1 | |
6238 | || TREE_INT_CST_LOW (position) > arg_count ) | |
6239 | { | |
6240 | warning (OPT_Wattributes, | |
6241 | "alloc_size parameter outside range"); | |
6242 | *no_add_attrs = true; | |
6243 | return NULL_TREE; | |
6244 | } | |
6245 | } | |
6246 | return NULL_TREE; | |
6247 | } | |
6248 | ||
26d1c5ff | 6249 | /* Handle a "returns_twice" attribute; arguments as in |
6250 | struct attribute_spec.handler. */ | |
6251 | ||
6252 | static tree | |
6253 | handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
6254 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6255 | { | |
6256 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6257 | DECL_IS_RETURNS_TWICE (*node) = 1; | |
6258 | else | |
6259 | { | |
9b2d6d13 | 6260 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
26d1c5ff | 6261 | *no_add_attrs = true; |
6262 | } | |
6263 | ||
6264 | return NULL_TREE; | |
6265 | } | |
6266 | ||
f8e93a2e | 6267 | /* Handle a "no_limit_stack" attribute; arguments as in |
6268 | struct attribute_spec.handler. */ | |
6269 | ||
6270 | static tree | |
1cae46be | 6271 | handle_no_limit_stack_attribute (tree *node, tree name, |
9a03a746 | 6272 | tree ARG_UNUSED (args), |
6273 | int ARG_UNUSED (flags), | |
09347743 | 6274 | bool *no_add_attrs) |
f8e93a2e | 6275 | { |
6276 | tree decl = *node; | |
6277 | ||
6278 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
6279 | { | |
b0b1af64 | 6280 | error ("%J%qE attribute applies only to functions", decl, name); |
f8e93a2e | 6281 | *no_add_attrs = true; |
6282 | } | |
6283 | else if (DECL_INITIAL (decl)) | |
6284 | { | |
b0b1af64 | 6285 | error ("%Jcan%'t set %qE attribute after definition", decl, name); |
f8e93a2e | 6286 | *no_add_attrs = true; |
6287 | } | |
6288 | else | |
6289 | DECL_NO_LIMIT_STACK (decl) = 1; | |
6290 | ||
6291 | return NULL_TREE; | |
6292 | } | |
6293 | ||
6294 | /* Handle a "pure" attribute; arguments as in | |
6295 | struct attribute_spec.handler. */ | |
6296 | ||
6297 | static tree | |
9a03a746 | 6298 | handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6299 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6300 | { |
6301 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
9c2a0c05 | 6302 | DECL_PURE_P (*node) = 1; |
f8e93a2e | 6303 | /* ??? TODO: Support types. */ |
6304 | else | |
6305 | { | |
9b2d6d13 | 6306 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6307 | *no_add_attrs = true; |
6308 | } | |
6309 | ||
6310 | return NULL_TREE; | |
6311 | } | |
6312 | ||
fc09b200 | 6313 | /* Handle a "no vops" attribute; arguments as in |
6314 | struct attribute_spec.handler. */ | |
6315 | ||
6316 | static tree | |
6317 | handle_novops_attribute (tree *node, tree ARG_UNUSED (name), | |
6318 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
6319 | bool *ARG_UNUSED (no_add_attrs)) | |
6320 | { | |
6321 | gcc_assert (TREE_CODE (*node) == FUNCTION_DECL); | |
6322 | DECL_IS_NOVOPS (*node) = 1; | |
6323 | return NULL_TREE; | |
6324 | } | |
6325 | ||
f8e93a2e | 6326 | /* Handle a "deprecated" attribute; arguments as in |
6327 | struct attribute_spec.handler. */ | |
1cae46be | 6328 | |
f8e93a2e | 6329 | static tree |
1cae46be | 6330 | handle_deprecated_attribute (tree *node, tree name, |
9a03a746 | 6331 | tree ARG_UNUSED (args), int flags, |
09347743 | 6332 | bool *no_add_attrs) |
f8e93a2e | 6333 | { |
6334 | tree type = NULL_TREE; | |
6335 | int warn = 0; | |
782858b8 | 6336 | tree what = NULL_TREE; |
1cae46be | 6337 | |
f8e93a2e | 6338 | if (DECL_P (*node)) |
6339 | { | |
6340 | tree decl = *node; | |
6341 | type = TREE_TYPE (decl); | |
1cae46be | 6342 | |
f8e93a2e | 6343 | if (TREE_CODE (decl) == TYPE_DECL |
6344 | || TREE_CODE (decl) == PARM_DECL | |
6345 | || TREE_CODE (decl) == VAR_DECL | |
6346 | || TREE_CODE (decl) == FUNCTION_DECL | |
6347 | || TREE_CODE (decl) == FIELD_DECL) | |
6348 | TREE_DEPRECATED (decl) = 1; | |
6349 | else | |
6350 | warn = 1; | |
6351 | } | |
6352 | else if (TYPE_P (*node)) | |
6353 | { | |
6354 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 6355 | *node = build_variant_type_copy (*node); |
f8e93a2e | 6356 | TREE_DEPRECATED (*node) = 1; |
6357 | type = *node; | |
6358 | } | |
6359 | else | |
6360 | warn = 1; | |
1cae46be | 6361 | |
f8e93a2e | 6362 | if (warn) |
6363 | { | |
6364 | *no_add_attrs = true; | |
6365 | if (type && TYPE_NAME (type)) | |
6366 | { | |
6367 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
782858b8 | 6368 | what = TYPE_NAME (*node); |
f8e93a2e | 6369 | else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL |
6370 | && DECL_NAME (TYPE_NAME (type))) | |
782858b8 | 6371 | what = DECL_NAME (TYPE_NAME (type)); |
f8e93a2e | 6372 | } |
6373 | if (what) | |
9b2d6d13 | 6374 | warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what); |
f8e93a2e | 6375 | else |
9b2d6d13 | 6376 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6377 | } |
6378 | ||
6379 | return NULL_TREE; | |
6380 | } | |
6381 | ||
f8e93a2e | 6382 | /* Handle a "vector_size" attribute; arguments as in |
6383 | struct attribute_spec.handler. */ | |
6384 | ||
6385 | static tree | |
1cae46be | 6386 | handle_vector_size_attribute (tree *node, tree name, tree args, |
9a03a746 | 6387 | int ARG_UNUSED (flags), |
09347743 | 6388 | bool *no_add_attrs) |
f8e93a2e | 6389 | { |
6390 | unsigned HOST_WIDE_INT vecsize, nunits; | |
83e2a11b | 6391 | enum machine_mode orig_mode; |
4917c376 | 6392 | tree type = *node, new_type, size; |
f8e93a2e | 6393 | |
6394 | *no_add_attrs = true; | |
6395 | ||
4917c376 | 6396 | size = TREE_VALUE (args); |
4917c376 | 6397 | |
84166705 | 6398 | if (!host_integerp (size, 1)) |
f8e93a2e | 6399 | { |
9b2d6d13 | 6400 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6401 | return NULL_TREE; |
6402 | } | |
6403 | ||
6404 | /* Get the vector size (in bytes). */ | |
4917c376 | 6405 | vecsize = tree_low_cst (size, 1); |
f8e93a2e | 6406 | |
6407 | /* We need to provide for vector pointers, vector arrays, and | |
6408 | functions returning vectors. For example: | |
6409 | ||
6410 | __attribute__((vector_size(16))) short *foo; | |
6411 | ||
6412 | In this case, the mode is SI, but the type being modified is | |
6413 | HI, so we need to look further. */ | |
6414 | ||
6415 | while (POINTER_TYPE_P (type) | |
6416 | || TREE_CODE (type) == FUNCTION_TYPE | |
5bfb0742 | 6417 | || TREE_CODE (type) == METHOD_TYPE |
2cb0e5d9 | 6418 | || TREE_CODE (type) == ARRAY_TYPE |
6419 | || TREE_CODE (type) == OFFSET_TYPE) | |
f8e93a2e | 6420 | type = TREE_TYPE (type); |
6421 | ||
6422 | /* Get the mode of the type being modified. */ | |
6423 | orig_mode = TYPE_MODE (type); | |
6424 | ||
2cb0e5d9 | 6425 | if ((!INTEGRAL_TYPE_P (type) |
6426 | && !SCALAR_FLOAT_TYPE_P (type) | |
6427 | && !FIXED_POINT_TYPE_P (type)) | |
cee7491d | 6428 | || (!SCALAR_FLOAT_MODE_P (orig_mode) |
9421ebb9 | 6429 | && GET_MODE_CLASS (orig_mode) != MODE_INT |
6430 | && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode)) | |
84166705 | 6431 | || !host_integerp (TYPE_SIZE_UNIT (type), 1)) |
f8e93a2e | 6432 | { |
782858b8 | 6433 | error ("invalid vector type for attribute %qE", name); |
f8e93a2e | 6434 | return NULL_TREE; |
6435 | } | |
6436 | ||
39cc3e6d | 6437 | if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1)) |
6438 | { | |
6439 | error ("vector size not an integral multiple of component size"); | |
6440 | return NULL; | |
6441 | } | |
6442 | ||
6443 | if (vecsize == 0) | |
6444 | { | |
6445 | error ("zero vector size"); | |
6446 | return NULL; | |
6447 | } | |
6448 | ||
f8e93a2e | 6449 | /* Calculate how many units fit in the vector. */ |
6450 | nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1); | |
83e2a11b | 6451 | if (nunits & (nunits - 1)) |
f8e93a2e | 6452 | { |
83e2a11b | 6453 | error ("number of components of the vector not a power of two"); |
f8e93a2e | 6454 | return NULL_TREE; |
6455 | } | |
6456 | ||
83e2a11b | 6457 | new_type = build_vector_type (type, nunits); |
f8e93a2e | 6458 | |
6459 | /* Build back pointers if needed. */ | |
d991e6e8 | 6460 | *node = lang_hooks.types.reconstruct_complex_type (*node, new_type); |
f8e93a2e | 6461 | |
6462 | return NULL_TREE; | |
6463 | } | |
6464 | ||
dbf6c367 | 6465 | /* Handle the "nonnull" attribute. */ |
6466 | static tree | |
9a03a746 | 6467 | handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), |
6468 | tree args, int ARG_UNUSED (flags), | |
09347743 | 6469 | bool *no_add_attrs) |
dbf6c367 | 6470 | { |
6471 | tree type = *node; | |
6472 | unsigned HOST_WIDE_INT attr_arg_num; | |
6473 | ||
6474 | /* If no arguments are specified, all pointer arguments should be | |
d716ce75 | 6475 | non-null. Verify a full prototype is given so that the arguments |
dbf6c367 | 6476 | will have the correct types when we actually check them later. */ |
84166705 | 6477 | if (!args) |
dbf6c367 | 6478 | { |
84166705 | 6479 | if (!TYPE_ARG_TYPES (type)) |
dbf6c367 | 6480 | { |
6481 | error ("nonnull attribute without arguments on a non-prototype"); | |
4ee9c684 | 6482 | *no_add_attrs = true; |
dbf6c367 | 6483 | } |
6484 | return NULL_TREE; | |
6485 | } | |
6486 | ||
6487 | /* Argument list specified. Verify that each argument number references | |
6488 | a pointer argument. */ | |
6489 | for (attr_arg_num = 1; args; args = TREE_CHAIN (args)) | |
6490 | { | |
6491 | tree argument; | |
4ee9c684 | 6492 | unsigned HOST_WIDE_INT arg_num = 0, ck_num; |
dbf6c367 | 6493 | |
84166705 | 6494 | if (!get_nonnull_operand (TREE_VALUE (args), &arg_num)) |
dbf6c367 | 6495 | { |
07e3a3d2 | 6496 | error ("nonnull argument has invalid operand number (argument %lu)", |
dbf6c367 | 6497 | (unsigned long) attr_arg_num); |
6498 | *no_add_attrs = true; | |
6499 | return NULL_TREE; | |
6500 | } | |
6501 | ||
6502 | argument = TYPE_ARG_TYPES (type); | |
6503 | if (argument) | |
6504 | { | |
6505 | for (ck_num = 1; ; ck_num++) | |
6506 | { | |
84166705 | 6507 | if (!argument || ck_num == arg_num) |
dbf6c367 | 6508 | break; |
6509 | argument = TREE_CHAIN (argument); | |
6510 | } | |
6511 | ||
84166705 | 6512 | if (!argument |
dbf6c367 | 6513 | || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE) |
6514 | { | |
07e3a3d2 | 6515 | error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)", |
dbf6c367 | 6516 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
6517 | *no_add_attrs = true; | |
6518 | return NULL_TREE; | |
6519 | } | |
6520 | ||
4ee9c684 | 6521 | if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE) |
dbf6c367 | 6522 | { |
07e3a3d2 | 6523 | error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)", |
dbf6c367 | 6524 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
6525 | *no_add_attrs = true; | |
6526 | return NULL_TREE; | |
6527 | } | |
6528 | } | |
6529 | } | |
6530 | ||
6531 | return NULL_TREE; | |
6532 | } | |
6533 | ||
6534 | /* Check the argument list of a function call for null in argument slots | |
d01f58f9 | 6535 | that are marked as requiring a non-null pointer argument. The NARGS |
6536 | arguments are passed in the array ARGARRAY. | |
6537 | */ | |
dbf6c367 | 6538 | |
6539 | static void | |
d01f58f9 | 6540 | check_function_nonnull (tree attrs, int nargs, tree *argarray) |
dbf6c367 | 6541 | { |
d01f58f9 | 6542 | tree a, args; |
6543 | int i; | |
dbf6c367 | 6544 | |
6545 | for (a = attrs; a; a = TREE_CHAIN (a)) | |
6546 | { | |
6547 | if (is_attribute_p ("nonnull", TREE_PURPOSE (a))) | |
6548 | { | |
4ee9c684 | 6549 | args = TREE_VALUE (a); |
dbf6c367 | 6550 | |
4ee9c684 | 6551 | /* Walk the argument list. If we encounter an argument number we |
6552 | should check for non-null, do it. If the attribute has no args, | |
6553 | then every pointer argument is checked (in which case the check | |
dbf6c367 | 6554 | for pointer type is done in check_nonnull_arg). */ |
d01f58f9 | 6555 | for (i = 0; i < nargs; i++) |
4ee9c684 | 6556 | { |
d01f58f9 | 6557 | if (!args || nonnull_check_p (args, i + 1)) |
e4603171 | 6558 | check_function_arguments_recurse (check_nonnull_arg, NULL, |
d01f58f9 | 6559 | argarray[i], |
6560 | i + 1); | |
4ee9c684 | 6561 | } |
dbf6c367 | 6562 | } |
6563 | } | |
6564 | } | |
6565 | ||
50ca527f | 6566 | /* Check that the Nth argument of a function call (counting backwards |
d01f58f9 | 6567 | from the end) is a (pointer)0. The NARGS arguments are passed in the |
6568 | array ARGARRAY. */ | |
bf6c8de0 | 6569 | |
6570 | static void | |
d01f58f9 | 6571 | check_function_sentinel (tree attrs, int nargs, tree *argarray, tree typelist) |
bf6c8de0 | 6572 | { |
6573 | tree attr = lookup_attribute ("sentinel", attrs); | |
6574 | ||
6575 | if (attr) | |
6576 | { | |
d01f58f9 | 6577 | int len = 0; |
6578 | int pos = 0; | |
6579 | tree sentinel; | |
a0c938f0 | 6580 | |
d01f58f9 | 6581 | /* Skip over the named arguments. */ |
6582 | while (typelist && len < nargs) | |
a0c938f0 | 6583 | { |
d01f58f9 | 6584 | typelist = TREE_CHAIN (typelist); |
6585 | len++; | |
6586 | } | |
50ca527f | 6587 | |
d01f58f9 | 6588 | if (TREE_VALUE (attr)) |
6589 | { | |
6590 | tree p = TREE_VALUE (TREE_VALUE (attr)); | |
6591 | pos = TREE_INT_CST_LOW (p); | |
6592 | } | |
50ca527f | 6593 | |
d01f58f9 | 6594 | /* The sentinel must be one of the varargs, i.e. |
6595 | in position >= the number of fixed arguments. */ | |
6596 | if ((nargs - 1 - pos) < len) | |
6597 | { | |
6598 | warning (OPT_Wformat, | |
6599 | "not enough variable arguments to fit a sentinel"); | |
6600 | return; | |
bf6c8de0 | 6601 | } |
d01f58f9 | 6602 | |
6603 | /* Validate the sentinel. */ | |
6604 | sentinel = argarray[nargs - 1 - pos]; | |
6605 | if ((!POINTER_TYPE_P (TREE_TYPE (sentinel)) | |
6606 | || !integer_zerop (sentinel)) | |
6607 | /* Although __null (in C++) is only an integer we allow it | |
6608 | nevertheless, as we are guaranteed that it's exactly | |
6609 | as wide as a pointer, and we don't want to force | |
6610 | users to cast the NULL they have written there. | |
6611 | We warn with -Wstrict-null-sentinel, though. */ | |
6612 | && (warn_strict_null_sentinel || null_node != sentinel)) | |
6613 | warning (OPT_Wformat, "missing sentinel in function call"); | |
bf6c8de0 | 6614 | } |
6615 | } | |
6616 | ||
dbf6c367 | 6617 | /* Helper for check_function_nonnull; given a list of operands which |
6618 | must be non-null in ARGS, determine if operand PARAM_NUM should be | |
6619 | checked. */ | |
6620 | ||
6621 | static bool | |
1cae46be | 6622 | nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 6623 | { |
4ee9c684 | 6624 | unsigned HOST_WIDE_INT arg_num = 0; |
dbf6c367 | 6625 | |
6626 | for (; args; args = TREE_CHAIN (args)) | |
6627 | { | |
231bd014 | 6628 | bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num); |
6629 | ||
6630 | gcc_assert (found); | |
dbf6c367 | 6631 | |
6632 | if (arg_num == param_num) | |
6633 | return true; | |
6634 | } | |
6635 | return false; | |
6636 | } | |
6637 | ||
6638 | /* Check that the function argument PARAM (which is operand number | |
6639 | PARAM_NUM) is non-null. This is called by check_function_nonnull | |
6640 | via check_function_arguments_recurse. */ | |
6641 | ||
6642 | static void | |
9a03a746 | 6643 | check_nonnull_arg (void * ARG_UNUSED (ctx), tree param, |
1cae46be | 6644 | unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 6645 | { |
6646 | /* Just skip checking the argument if it's not a pointer. This can | |
6647 | happen if the "nonnull" attribute was given without an operand | |
6648 | list (which means to check every pointer argument). */ | |
6649 | ||
6650 | if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) | |
6651 | return; | |
6652 | ||
6653 | if (integer_zerop (param)) | |
155b601b | 6654 | warning (OPT_Wnonnull, "null argument where non-null required " |
6655 | "(argument %lu)", (unsigned long) param_num); | |
dbf6c367 | 6656 | } |
6657 | ||
6658 | /* Helper for nonnull attribute handling; fetch the operand number | |
6659 | from the attribute argument list. */ | |
6660 | ||
6661 | static bool | |
1cae46be | 6662 | get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) |
dbf6c367 | 6663 | { |
67409385 | 6664 | /* Verify the arg number is a constant. */ |
dbf6c367 | 6665 | if (TREE_CODE (arg_num_expr) != INTEGER_CST |
6666 | || TREE_INT_CST_HIGH (arg_num_expr) != 0) | |
6667 | return false; | |
6668 | ||
6669 | *valp = TREE_INT_CST_LOW (arg_num_expr); | |
6670 | return true; | |
6671 | } | |
fa987697 | 6672 | |
6673 | /* Handle a "nothrow" attribute; arguments as in | |
6674 | struct attribute_spec.handler. */ | |
6675 | ||
6676 | static tree | |
9a03a746 | 6677 | handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6678 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
fa987697 | 6679 | { |
6680 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6681 | TREE_NOTHROW (*node) = 1; | |
6682 | /* ??? TODO: Support types. */ | |
6683 | else | |
6684 | { | |
9b2d6d13 | 6685 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
fa987697 | 6686 | *no_add_attrs = true; |
6687 | } | |
6688 | ||
6689 | return NULL_TREE; | |
6690 | } | |
7acb29a3 | 6691 | |
6692 | /* Handle a "cleanup" attribute; arguments as in | |
6693 | struct attribute_spec.handler. */ | |
6694 | ||
6695 | static tree | |
1cae46be | 6696 | handle_cleanup_attribute (tree *node, tree name, tree args, |
9a03a746 | 6697 | int ARG_UNUSED (flags), bool *no_add_attrs) |
7acb29a3 | 6698 | { |
6699 | tree decl = *node; | |
6700 | tree cleanup_id, cleanup_decl; | |
6701 | ||
6702 | /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do | |
6703 | for global destructors in C++. This requires infrastructure that | |
6704 | we don't have generically at the moment. It's also not a feature | |
6705 | we'd be missing too much, since we do have attribute constructor. */ | |
6706 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
6707 | { | |
9b2d6d13 | 6708 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
7acb29a3 | 6709 | *no_add_attrs = true; |
6710 | return NULL_TREE; | |
6711 | } | |
6712 | ||
6713 | /* Verify that the argument is a function in scope. */ | |
6714 | /* ??? We could support pointers to functions here as well, if | |
6715 | that was considered desirable. */ | |
6716 | cleanup_id = TREE_VALUE (args); | |
6717 | if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE) | |
6718 | { | |
07e3a3d2 | 6719 | error ("cleanup argument not an identifier"); |
7acb29a3 | 6720 | *no_add_attrs = true; |
6721 | return NULL_TREE; | |
6722 | } | |
d1c41717 | 6723 | cleanup_decl = lookup_name (cleanup_id); |
7acb29a3 | 6724 | if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL) |
6725 | { | |
07e3a3d2 | 6726 | error ("cleanup argument not a function"); |
7acb29a3 | 6727 | *no_add_attrs = true; |
6728 | return NULL_TREE; | |
6729 | } | |
6730 | ||
1cae46be | 6731 | /* That the function has proper type is checked with the |
7acb29a3 | 6732 | eventual call to build_function_call. */ |
6733 | ||
6734 | return NULL_TREE; | |
6735 | } | |
8a8cdb8d | 6736 | |
6737 | /* Handle a "warn_unused_result" attribute. No special handling. */ | |
6738 | ||
6739 | static tree | |
6740 | handle_warn_unused_result_attribute (tree *node, tree name, | |
9a03a746 | 6741 | tree ARG_UNUSED (args), |
6742 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8a8cdb8d | 6743 | { |
6744 | /* Ignore the attribute for functions not returning any value. */ | |
6745 | if (VOID_TYPE_P (TREE_TYPE (*node))) | |
6746 | { | |
9b2d6d13 | 6747 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
8a8cdb8d | 6748 | *no_add_attrs = true; |
6749 | } | |
6750 | ||
6751 | return NULL_TREE; | |
6752 | } | |
bf6c8de0 | 6753 | |
6754 | /* Handle a "sentinel" attribute. */ | |
6755 | ||
6756 | static tree | |
50ca527f | 6757 | handle_sentinel_attribute (tree *node, tree name, tree args, |
bf6c8de0 | 6758 | int ARG_UNUSED (flags), bool *no_add_attrs) |
6759 | { | |
6760 | tree params = TYPE_ARG_TYPES (*node); | |
6761 | ||
6762 | if (!params) | |
6763 | { | |
9b2d6d13 | 6764 | warning (OPT_Wattributes, |
6765 | "%qE attribute requires prototypes with named arguments", name); | |
bf6c8de0 | 6766 | *no_add_attrs = true; |
bf6c8de0 | 6767 | } |
50ca527f | 6768 | else |
6769 | { | |
6770 | while (TREE_CHAIN (params)) | |
6771 | params = TREE_CHAIN (params); | |
bf6c8de0 | 6772 | |
50ca527f | 6773 | if (VOID_TYPE_P (TREE_VALUE (params))) |
a0c938f0 | 6774 | { |
9b2d6d13 | 6775 | warning (OPT_Wattributes, |
6776 | "%qE attribute only applies to variadic functions", name); | |
50ca527f | 6777 | *no_add_attrs = true; |
6778 | } | |
6779 | } | |
a0c938f0 | 6780 | |
50ca527f | 6781 | if (args) |
bf6c8de0 | 6782 | { |
50ca527f | 6783 | tree position = TREE_VALUE (args); |
6784 | ||
50ca527f | 6785 | if (TREE_CODE (position) != INTEGER_CST) |
a0c938f0 | 6786 | { |
01b54db5 | 6787 | warning (OPT_Wattributes, |
6788 | "requested position is not an integer constant"); | |
50ca527f | 6789 | *no_add_attrs = true; |
6790 | } | |
6791 | else | |
a0c938f0 | 6792 | { |
50ca527f | 6793 | if (tree_int_cst_lt (position, integer_zero_node)) |
6794 | { | |
01b54db5 | 6795 | warning (OPT_Wattributes, |
6796 | "requested position is less than zero"); | |
50ca527f | 6797 | *no_add_attrs = true; |
6798 | } | |
6799 | } | |
bf6c8de0 | 6800 | } |
a0c938f0 | 6801 | |
bf6c8de0 | 6802 | return NULL_TREE; |
6803 | } | |
b5c26b42 | 6804 | |
6805 | /* Handle a "type_generic" attribute. */ | |
6806 | ||
6807 | static tree | |
6808 | handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name), | |
6809 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
6810 | bool * ARG_UNUSED (no_add_attrs)) | |
6811 | { | |
19fbe3a4 | 6812 | tree params; |
6813 | ||
6814 | /* Ensure we have a function type. */ | |
6815 | gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE); | |
6816 | ||
6817 | params = TYPE_ARG_TYPES (*node); | |
6818 | while (params && ! VOID_TYPE_P (TREE_VALUE (params))) | |
6819 | params = TREE_CHAIN (params); | |
6820 | ||
6821 | /* Ensure we have a variadic function. */ | |
6822 | gcc_assert (!params); | |
b5c26b42 | 6823 | |
6824 | return NULL_TREE; | |
6825 | } | |
46f8e3b0 | 6826 | |
6827 | /* For handling "option" attribute. arguments as in | |
6828 | struct attribute_spec.handler. */ | |
6829 | ||
6830 | static tree | |
6831 | handle_option_attribute (tree *node, tree name, tree args, int flags, | |
6832 | bool *no_add_attrs) | |
6833 | { | |
6834 | /* Ensure we have a function type. */ | |
6835 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6836 | { | |
6837 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6838 | *no_add_attrs = true; | |
6839 | } | |
6840 | else if (targetm.target_option.valid_attribute_p | |
6841 | == default_target_option_valid_attribute_p) | |
6842 | { | |
6843 | warning (OPT_Wattributes, | |
6844 | "%qE attribute is not supported on this machine", | |
6845 | name); | |
6846 | *no_add_attrs = true; | |
6847 | } | |
6848 | else if (! targetm.target_option.valid_attribute_p (*node, name, args, | |
6849 | flags)) | |
6850 | *no_add_attrs = true; | |
6851 | ||
6852 | return NULL_TREE; | |
6853 | } | |
6854 | ||
6855 | /* Arguments being collected for optimization. */ | |
6856 | typedef const char *const_char_p; /* For DEF_VEC_P. */ | |
6857 | DEF_VEC_P(const_char_p); | |
6858 | DEF_VEC_ALLOC_P(const_char_p, gc); | |
6859 | static GTY(()) VEC(const_char_p, gc) *optimize_args; | |
6860 | ||
6861 | ||
6862 | /* Inner function to convert a TREE_LIST to argv string to parse the optimize | |
6863 | options in ARGS. ATTR_P is true if this is for attribute(optimize), and | |
6864 | false for #pragma GCC optimize. */ | |
6865 | ||
6866 | bool | |
6867 | parse_optimize_options (tree args, bool attr_p) | |
6868 | { | |
6869 | bool ret = true; | |
6870 | unsigned opt_argc; | |
6871 | unsigned i; | |
6872 | const char **opt_argv; | |
6873 | tree ap; | |
6874 | ||
6875 | /* Build up argv vector. Just in case the string is stored away, use garbage | |
6876 | collected strings. */ | |
6877 | VEC_truncate (const_char_p, optimize_args, 0); | |
6878 | VEC_safe_push (const_char_p, gc, optimize_args, NULL); | |
6879 | ||
6880 | for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap)) | |
6881 | { | |
6882 | tree value = TREE_VALUE (ap); | |
6883 | ||
6884 | if (TREE_CODE (value) == INTEGER_CST) | |
6885 | { | |
6886 | char buffer[20]; | |
6887 | sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value)); | |
6888 | VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer)); | |
6889 | } | |
6890 | ||
6891 | else if (TREE_CODE (value) == STRING_CST) | |
6892 | { | |
6893 | /* Split string into multiple substrings. */ | |
6894 | size_t len = TREE_STRING_LENGTH (value); | |
6895 | char *p = ASTRDUP (TREE_STRING_POINTER (value)); | |
6896 | char *end = p + len; | |
6897 | char *comma; | |
6898 | char *next_p = p; | |
6899 | ||
6900 | while (next_p != NULL) | |
6901 | { | |
6902 | size_t len2; | |
6903 | char *q, *r; | |
6904 | ||
6905 | p = next_p; | |
6906 | comma = strchr (p, ','); | |
6907 | if (comma) | |
6908 | { | |
6909 | len2 = comma - p; | |
6910 | *comma = '\0'; | |
6911 | next_p = comma+1; | |
6912 | } | |
6913 | else | |
6914 | { | |
6915 | len2 = end - p; | |
6916 | next_p = NULL; | |
6917 | } | |
6918 | ||
6919 | r = q = (char *) ggc_alloc (len2 + 3); | |
6920 | ||
6921 | /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx | |
6922 | options. */ | |
6923 | if (*p == '-' && p[1] != 'O' && p[1] != 'f') | |
6924 | { | |
6925 | ret = false; | |
6926 | if (attr_p) | |
6927 | warning (OPT_Wattributes, | |
6928 | "Bad option %s to optimize attribute.", p); | |
6929 | else | |
6930 | warning (OPT_Wpragmas, | |
6931 | "Bad option %s to pragma attribute", p); | |
6932 | continue; | |
6933 | } | |
6934 | ||
6935 | if (*p != '-') | |
6936 | { | |
6937 | *r++ = '-'; | |
6938 | ||
6939 | /* Assume that Ox is -Ox, a numeric value is -Ox, a s by | |
6940 | itself is -Os, and any other switch begins with a -f. */ | |
6941 | if ((*p >= '0' && *p <= '9') | |
6942 | || (p[0] == 's' && p[1] == '\0')) | |
6943 | *r++ = 'O'; | |
6944 | else if (*p != 'O') | |
6945 | *r++ = 'f'; | |
6946 | } | |
6947 | ||
6948 | memcpy (r, p, len2); | |
6949 | r[len2] = '\0'; | |
6950 | VEC_safe_push (const_char_p, gc, optimize_args, q); | |
6951 | } | |
6952 | ||
6953 | } | |
6954 | } | |
6955 | ||
6956 | opt_argc = VEC_length (const_char_p, optimize_args); | |
6957 | opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1)); | |
6958 | ||
6959 | for (i = 1; i < opt_argc; i++) | |
6960 | opt_argv[i] = VEC_index (const_char_p, optimize_args, i); | |
6961 | ||
6962 | /* Now parse the options. */ | |
6963 | decode_options (opt_argc, opt_argv); | |
6964 | ||
6965 | VEC_truncate (const_char_p, optimize_args, 0); | |
6966 | return ret; | |
6967 | } | |
6968 | ||
6969 | /* For handling "optimize" attribute. arguments as in | |
6970 | struct attribute_spec.handler. */ | |
6971 | ||
6972 | static tree | |
6973 | handle_optimize_attribute (tree *node, tree name, tree args, | |
6974 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6975 | { | |
6976 | /* Ensure we have a function type. */ | |
6977 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6978 | { | |
6979 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6980 | *no_add_attrs = true; | |
6981 | } | |
6982 | else | |
6983 | { | |
6984 | struct cl_optimization cur_opts; | |
6985 | tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node); | |
6986 | ||
6987 | /* Save current options. */ | |
6988 | cl_optimization_save (&cur_opts); | |
6989 | ||
6990 | /* If we previously had some optimization options, use them as the | |
6991 | default. */ | |
6992 | if (old_opts) | |
6993 | cl_optimization_restore (TREE_OPTIMIZATION (old_opts)); | |
6994 | ||
6995 | /* Parse options, and update the vector. */ | |
6996 | parse_optimize_options (args, true); | |
6997 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) | |
6998 | = build_optimization_node (); | |
6999 | ||
7000 | /* Restore current options. */ | |
7001 | cl_optimization_restore (&cur_opts); | |
7002 | } | |
7003 | ||
7004 | return NULL_TREE; | |
7005 | } | |
dbf6c367 | 7006 | \f |
d01f58f9 | 7007 | /* Check for valid arguments being passed to a function. |
7008 | ATTRS is a list of attributes. There are NARGS arguments in the array | |
7009 | ARGARRAY. TYPELIST is the list of argument types for the function. | |
7010 | */ | |
dbf6c367 | 7011 | void |
d01f58f9 | 7012 | check_function_arguments (tree attrs, int nargs, tree *argarray, tree typelist) |
dbf6c367 | 7013 | { |
7014 | /* Check for null being passed in a pointer argument that must be | |
7015 | non-null. We also need to do this if format checking is enabled. */ | |
7016 | ||
7017 | if (warn_nonnull) | |
d01f58f9 | 7018 | check_function_nonnull (attrs, nargs, argarray); |
dbf6c367 | 7019 | |
7020 | /* Check for errors in format strings. */ | |
7021 | ||
95c90e04 | 7022 | if (warn_format || warn_missing_format_attribute) |
d01f58f9 | 7023 | check_function_format (attrs, nargs, argarray); |
95c90e04 | 7024 | |
7025 | if (warn_format) | |
d01f58f9 | 7026 | check_function_sentinel (attrs, nargs, argarray, typelist); |
dbf6c367 | 7027 | } |
7028 | ||
7029 | /* Generic argument checking recursion routine. PARAM is the argument to | |
7030 | be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked | |
7031 | once the argument is resolved. CTX is context for the callback. */ | |
7032 | void | |
1cae46be | 7033 | check_function_arguments_recurse (void (*callback) |
7034 | (void *, tree, unsigned HOST_WIDE_INT), | |
7035 | void *ctx, tree param, | |
7036 | unsigned HOST_WIDE_INT param_num) | |
dbf6c367 | 7037 | { |
72dd6141 | 7038 | if (CONVERT_EXPR_P (param) |
c44afe23 | 7039 | && (TYPE_PRECISION (TREE_TYPE (param)) |
7040 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0))))) | |
dbf6c367 | 7041 | { |
7042 | /* Strip coercion. */ | |
7043 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 7044 | TREE_OPERAND (param, 0), param_num); |
dbf6c367 | 7045 | return; |
7046 | } | |
7047 | ||
7048 | if (TREE_CODE (param) == CALL_EXPR) | |
7049 | { | |
c2f47e15 | 7050 | tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param))); |
dbf6c367 | 7051 | tree attrs; |
7052 | bool found_format_arg = false; | |
7053 | ||
7054 | /* See if this is a call to a known internationalization function | |
7055 | that modifies a format arg. Such a function may have multiple | |
7056 | format_arg attributes (for example, ngettext). */ | |
7057 | ||
7058 | for (attrs = TYPE_ATTRIBUTES (type); | |
7059 | attrs; | |
7060 | attrs = TREE_CHAIN (attrs)) | |
7061 | if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs))) | |
7062 | { | |
c2f47e15 | 7063 | tree inner_arg; |
dbf6c367 | 7064 | tree format_num_expr; |
7065 | int format_num; | |
7066 | int i; | |
c2f47e15 | 7067 | call_expr_arg_iterator iter; |
dbf6c367 | 7068 | |
7069 | /* Extract the argument number, which was previously checked | |
7070 | to be valid. */ | |
7071 | format_num_expr = TREE_VALUE (TREE_VALUE (attrs)); | |
dbf6c367 | 7072 | |
231bd014 | 7073 | gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST |
7074 | && !TREE_INT_CST_HIGH (format_num_expr)); | |
dbf6c367 | 7075 | |
7076 | format_num = TREE_INT_CST_LOW (format_num_expr); | |
7077 | ||
c2f47e15 | 7078 | for (inner_arg = first_call_expr_arg (param, &iter), i = 1; |
7079 | inner_arg != 0; | |
7080 | inner_arg = next_call_expr_arg (&iter), i++) | |
dbf6c367 | 7081 | if (i == format_num) |
7082 | { | |
7083 | check_function_arguments_recurse (callback, ctx, | |
c2f47e15 | 7084 | inner_arg, param_num); |
dbf6c367 | 7085 | found_format_arg = true; |
7086 | break; | |
7087 | } | |
7088 | } | |
7089 | ||
7090 | /* If we found a format_arg attribute and did a recursive check, | |
7091 | we are done with checking this argument. Otherwise, we continue | |
7092 | and this will be considered a non-literal. */ | |
7093 | if (found_format_arg) | |
7094 | return; | |
7095 | } | |
7096 | ||
7097 | if (TREE_CODE (param) == COND_EXPR) | |
7098 | { | |
7099 | /* Check both halves of the conditional expression. */ | |
7100 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 7101 | TREE_OPERAND (param, 1), param_num); |
dbf6c367 | 7102 | check_function_arguments_recurse (callback, ctx, |
4ee9c684 | 7103 | TREE_OPERAND (param, 2), param_num); |
dbf6c367 | 7104 | return; |
7105 | } | |
7106 | ||
7107 | (*callback) (ctx, param, param_num); | |
7108 | } | |
1f3233d1 | 7109 | |
d43cee80 | 7110 | /* Checks the number of arguments NARGS against the required number |
7111 | REQUIRED and issues an error if there is a mismatch. Returns true | |
7112 | if the number of arguments is correct, otherwise false. */ | |
7113 | ||
7114 | static bool | |
7115 | validate_nargs (tree fndecl, int nargs, int required) | |
7116 | { | |
7117 | if (nargs < required) | |
7118 | { | |
7119 | error ("not enough arguments to function %qE", fndecl); | |
7120 | return false; | |
7121 | } | |
7122 | else if (nargs > required) | |
7123 | { | |
7124 | error ("too many arguments to function %qE", fndecl); | |
7125 | return false; | |
7126 | } | |
7127 | return true; | |
7128 | } | |
7129 | ||
7130 | /* Verifies the NARGS arguments ARGS to the builtin function FNDECL. | |
7131 | Returns false if there was an error, otherwise true. */ | |
7132 | ||
7133 | bool | |
7134 | check_builtin_function_arguments (tree fndecl, int nargs, tree *args) | |
7135 | { | |
7136 | if (!DECL_BUILT_IN (fndecl) | |
7137 | || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) | |
7138 | return true; | |
7139 | ||
7140 | switch (DECL_FUNCTION_CODE (fndecl)) | |
7141 | { | |
7142 | case BUILT_IN_CONSTANT_P: | |
7143 | return validate_nargs (fndecl, nargs, 1); | |
7144 | ||
7145 | case BUILT_IN_ISFINITE: | |
7146 | case BUILT_IN_ISINF: | |
c319d56a | 7147 | case BUILT_IN_ISINF_SIGN: |
d43cee80 | 7148 | case BUILT_IN_ISNAN: |
7149 | case BUILT_IN_ISNORMAL: | |
7150 | if (validate_nargs (fndecl, nargs, 1)) | |
7151 | { | |
7152 | if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE) | |
7153 | { | |
7154 | error ("non-floating-point argument in call to " | |
7155 | "function %qE", fndecl); | |
7156 | return false; | |
7157 | } | |
7158 | return true; | |
7159 | } | |
7160 | return false; | |
7161 | ||
7162 | case BUILT_IN_ISGREATER: | |
7163 | case BUILT_IN_ISGREATEREQUAL: | |
7164 | case BUILT_IN_ISLESS: | |
7165 | case BUILT_IN_ISLESSEQUAL: | |
7166 | case BUILT_IN_ISLESSGREATER: | |
7167 | case BUILT_IN_ISUNORDERED: | |
7168 | if (validate_nargs (fndecl, nargs, 2)) | |
7169 | { | |
7170 | enum tree_code code0, code1; | |
7171 | code0 = TREE_CODE (TREE_TYPE (args[0])); | |
7172 | code1 = TREE_CODE (TREE_TYPE (args[1])); | |
7173 | if (!((code0 == REAL_TYPE && code1 == REAL_TYPE) | |
7174 | || (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
7175 | || (code0 == INTEGER_TYPE && code1 == REAL_TYPE))) | |
7176 | { | |
7177 | error ("non-floating-point arguments in call to " | |
7178 | "function %qE", fndecl); | |
7179 | return false; | |
7180 | } | |
7181 | return true; | |
7182 | } | |
7183 | return false; | |
7184 | ||
19fbe3a4 | 7185 | case BUILT_IN_FPCLASSIFY: |
7186 | if (validate_nargs (fndecl, nargs, 6)) | |
7187 | { | |
7188 | unsigned i; | |
7189 | ||
7190 | for (i=0; i<5; i++) | |
7191 | if (TREE_CODE (args[i]) != INTEGER_CST) | |
7192 | { | |
7193 | error ("non-const integer argument %u in call to function %qE", | |
7194 | i+1, fndecl); | |
7195 | return false; | |
7196 | } | |
7197 | ||
7198 | if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE) | |
7199 | { | |
7200 | error ("non-floating-point argument in call to function %qE", | |
7201 | fndecl); | |
7202 | return false; | |
7203 | } | |
7204 | return true; | |
7205 | } | |
7206 | return false; | |
7207 | ||
d43cee80 | 7208 | default: |
7209 | return true; | |
7210 | } | |
7211 | } | |
7212 | ||
860251be | 7213 | /* Function to help qsort sort FIELD_DECLs by name order. */ |
7214 | ||
7215 | int | |
7216 | field_decl_cmp (const void *x_p, const void *y_p) | |
7217 | { | |
4fd61bc6 | 7218 | const tree *const x = (const tree *const) x_p; |
7219 | const tree *const y = (const tree *const) y_p; | |
7220 | ||
860251be | 7221 | if (DECL_NAME (*x) == DECL_NAME (*y)) |
7222 | /* A nontype is "greater" than a type. */ | |
7223 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
7224 | if (DECL_NAME (*x) == NULL_TREE) | |
7225 | return -1; | |
7226 | if (DECL_NAME (*y) == NULL_TREE) | |
7227 | return 1; | |
7228 | if (DECL_NAME (*x) < DECL_NAME (*y)) | |
7229 | return -1; | |
7230 | return 1; | |
7231 | } | |
7232 | ||
7233 | static struct { | |
7234 | gt_pointer_operator new_value; | |
7235 | void *cookie; | |
7236 | } resort_data; | |
7237 | ||
7238 | /* This routine compares two fields like field_decl_cmp but using the | |
7239 | pointer operator in resort_data. */ | |
7240 | ||
7241 | static int | |
7242 | resort_field_decl_cmp (const void *x_p, const void *y_p) | |
7243 | { | |
4fd61bc6 | 7244 | const tree *const x = (const tree *const) x_p; |
7245 | const tree *const y = (const tree *const) y_p; | |
860251be | 7246 | |
7247 | if (DECL_NAME (*x) == DECL_NAME (*y)) | |
7248 | /* A nontype is "greater" than a type. */ | |
7249 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
7250 | if (DECL_NAME (*x) == NULL_TREE) | |
7251 | return -1; | |
7252 | if (DECL_NAME (*y) == NULL_TREE) | |
7253 | return 1; | |
7254 | { | |
7255 | tree d1 = DECL_NAME (*x); | |
7256 | tree d2 = DECL_NAME (*y); | |
7257 | resort_data.new_value (&d1, resort_data.cookie); | |
7258 | resort_data.new_value (&d2, resort_data.cookie); | |
7259 | if (d1 < d2) | |
7260 | return -1; | |
7261 | } | |
7262 | return 1; | |
7263 | } | |
7264 | ||
7265 | /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ | |
7266 | ||
7267 | void | |
7268 | resort_sorted_fields (void *obj, | |
9a03a746 | 7269 | void * ARG_UNUSED (orig_obj), |
4ee9c684 | 7270 | gt_pointer_operator new_value, |
7271 | void *cookie) | |
860251be | 7272 | { |
9a03a746 | 7273 | struct sorted_fields_type *sf = (struct sorted_fields_type *) obj; |
860251be | 7274 | resort_data.new_value = new_value; |
7275 | resort_data.cookie = cookie; | |
7276 | qsort (&sf->elts[0], sf->len, sizeof (tree), | |
4ee9c684 | 7277 | resort_field_decl_cmp); |
860251be | 7278 | } |
7279 | ||
209c9752 | 7280 | /* Subroutine of c_parse_error. |
7281 | Return the result of concatenating LHS and RHS. RHS is really | |
7282 | a string literal, its first character is indicated by RHS_START and | |
cfee01e3 | 7283 | RHS_SIZE is its length (including the terminating NUL character). |
209c9752 | 7284 | |
7285 | The caller is responsible for deleting the returned pointer. */ | |
7286 | ||
7287 | static char * | |
7288 | catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) | |
7289 | { | |
7290 | const int lhs_size = strlen (lhs); | |
7291 | char *result = XNEWVEC (char, lhs_size + rhs_size); | |
7292 | strncpy (result, lhs, lhs_size); | |
7293 | strncpy (result + lhs_size, rhs_start, rhs_size); | |
7294 | return result; | |
7295 | } | |
7296 | ||
380c6697 | 7297 | /* Issue the error given by GMSGID, indicating that it occurred before |
92b128ed | 7298 | TOKEN, which had the associated VALUE. */ |
7299 | ||
7300 | void | |
380c6697 | 7301 | c_parse_error (const char *gmsgid, enum cpp_ttype token, tree value) |
92b128ed | 7302 | { |
209c9752 | 7303 | #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2)) |
7304 | ||
7305 | char *message = NULL; | |
92b128ed | 7306 | |
7307 | if (token == CPP_EOF) | |
380c6697 | 7308 | message = catenate_messages (gmsgid, " at end of input"); |
924bbf02 | 7309 | else if (token == CPP_CHAR || token == CPP_WCHAR || token == CPP_CHAR16 |
7310 | || token == CPP_CHAR32) | |
92b128ed | 7311 | { |
7312 | unsigned int val = TREE_INT_CST_LOW (value); | |
924bbf02 | 7313 | const char *prefix; |
7314 | ||
7315 | switch (token) | |
7316 | { | |
7317 | default: | |
7318 | prefix = ""; | |
7319 | break; | |
7320 | case CPP_WCHAR: | |
7321 | prefix = "L"; | |
7322 | break; | |
7323 | case CPP_CHAR16: | |
7324 | prefix = "u"; | |
7325 | break; | |
7326 | case CPP_CHAR32: | |
7327 | prefix = "U"; | |
7328 | break; | |
7329 | } | |
7330 | ||
92b128ed | 7331 | if (val <= UCHAR_MAX && ISGRAPH (val)) |
a0c938f0 | 7332 | message = catenate_messages (gmsgid, " before %s'%c'"); |
92b128ed | 7333 | else |
a0c938f0 | 7334 | message = catenate_messages (gmsgid, " before %s'\\x%x'"); |
209c9752 | 7335 | |
924bbf02 | 7336 | error (message, prefix, val); |
209c9752 | 7337 | free (message); |
7338 | message = NULL; | |
92b128ed | 7339 | } |
924bbf02 | 7340 | else if (token == CPP_STRING || token == CPP_WSTRING || token == CPP_STRING16 |
7341 | || token == CPP_STRING32) | |
380c6697 | 7342 | message = catenate_messages (gmsgid, " before string constant"); |
92b128ed | 7343 | else if (token == CPP_NUMBER) |
380c6697 | 7344 | message = catenate_messages (gmsgid, " before numeric constant"); |
92b128ed | 7345 | else if (token == CPP_NAME) |
209c9752 | 7346 | { |
380c6697 | 7347 | message = catenate_messages (gmsgid, " before %qE"); |
782858b8 | 7348 | error (message, value); |
209c9752 | 7349 | free (message); |
7350 | message = NULL; | |
7351 | } | |
b75b98aa | 7352 | else if (token == CPP_PRAGMA) |
7353 | message = catenate_messages (gmsgid, " before %<#pragma%>"); | |
7354 | else if (token == CPP_PRAGMA_EOL) | |
7355 | message = catenate_messages (gmsgid, " before end of line"); | |
92b128ed | 7356 | else if (token < N_TTYPES) |
209c9752 | 7357 | { |
380c6697 | 7358 | message = catenate_messages (gmsgid, " before %qs token"); |
209c9752 | 7359 | error (message, cpp_type2name (token)); |
7360 | free (message); | |
7361 | message = NULL; | |
7362 | } | |
92b128ed | 7363 | else |
380c6697 | 7364 | error (gmsgid); |
209c9752 | 7365 | |
7366 | if (message) | |
7367 | { | |
7368 | error (message); | |
7369 | free (message); | |
7370 | } | |
a0c938f0 | 7371 | #undef catenate_messages |
92b128ed | 7372 | } |
7373 | ||
4ee9c684 | 7374 | /* Walk a gimplified function and warn for functions whose return value is |
7375 | ignored and attribute((warn_unused_result)) is set. This is done before | |
b27ac6b5 | 7376 | inlining, so we don't have to worry about that. */ |
7377 | ||
4ee9c684 | 7378 | void |
7379 | c_warn_unused_result (tree *top_p) | |
7380 | { | |
7381 | tree t = *top_p; | |
7382 | tree_stmt_iterator i; | |
7383 | tree fdecl, ftype; | |
7384 | ||
7385 | switch (TREE_CODE (t)) | |
7386 | { | |
7387 | case STATEMENT_LIST: | |
7388 | for (i = tsi_start (*top_p); !tsi_end_p (i); tsi_next (&i)) | |
7389 | c_warn_unused_result (tsi_stmt_ptr (i)); | |
7390 | break; | |
7391 | ||
7392 | case COND_EXPR: | |
7393 | c_warn_unused_result (&COND_EXPR_THEN (t)); | |
7394 | c_warn_unused_result (&COND_EXPR_ELSE (t)); | |
7395 | break; | |
7396 | case BIND_EXPR: | |
7397 | c_warn_unused_result (&BIND_EXPR_BODY (t)); | |
7398 | break; | |
7399 | case TRY_FINALLY_EXPR: | |
7400 | case TRY_CATCH_EXPR: | |
7401 | c_warn_unused_result (&TREE_OPERAND (t, 0)); | |
7402 | c_warn_unused_result (&TREE_OPERAND (t, 1)); | |
7403 | break; | |
7404 | case CATCH_EXPR: | |
7405 | c_warn_unused_result (&CATCH_BODY (t)); | |
7406 | break; | |
7407 | case EH_FILTER_EXPR: | |
7408 | c_warn_unused_result (&EH_FILTER_FAILURE (t)); | |
7409 | break; | |
7410 | ||
7411 | case CALL_EXPR: | |
0e364d12 | 7412 | if (TREE_USED (t)) |
7413 | break; | |
7414 | ||
4ee9c684 | 7415 | /* This is a naked call, as opposed to a CALL_EXPR nested inside |
7416 | a MODIFY_EXPR. All calls whose value is ignored should be | |
7417 | represented like this. Look for the attribute. */ | |
7418 | fdecl = get_callee_fndecl (t); | |
7419 | if (fdecl) | |
7420 | ftype = TREE_TYPE (fdecl); | |
7421 | else | |
7422 | { | |
c2f47e15 | 7423 | ftype = TREE_TYPE (CALL_EXPR_FN (t)); |
4ee9c684 | 7424 | /* Look past pointer-to-function to the function type itself. */ |
7425 | ftype = TREE_TYPE (ftype); | |
7426 | } | |
7427 | ||
7428 | if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype))) | |
7429 | { | |
7430 | if (fdecl) | |
c3ceba8e | 7431 | warning (0, "%Hignoring return value of %qD, " |
4ee9c684 | 7432 | "declared with attribute warn_unused_result", |
7433 | EXPR_LOCUS (t), fdecl); | |
7434 | else | |
c3ceba8e | 7435 | warning (0, "%Hignoring return value of function " |
4ee9c684 | 7436 | "declared with attribute warn_unused_result", |
7437 | EXPR_LOCUS (t)); | |
7438 | } | |
7439 | break; | |
7440 | ||
7441 | default: | |
7442 | /* Not a container, not a call, or a call whose value is used. */ | |
7443 | break; | |
7444 | } | |
7445 | } | |
7446 | ||
624d37a6 | 7447 | /* Convert a character from the host to the target execution character |
7448 | set. cpplib handles this, mostly. */ | |
7449 | ||
7450 | HOST_WIDE_INT | |
7451 | c_common_to_target_charset (HOST_WIDE_INT c) | |
7452 | { | |
7453 | /* Character constants in GCC proper are sign-extended under -fsigned-char, | |
7454 | zero-extended under -fno-signed-char. cpplib insists that characters | |
7455 | and character constants are always unsigned. Hence we must convert | |
7456 | back and forth. */ | |
7457 | cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1); | |
7458 | ||
7459 | uc = cpp_host_to_exec_charset (parse_in, uc); | |
7460 | ||
7461 | if (flag_signed_char) | |
7462 | return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE) | |
7463 | >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE); | |
7464 | else | |
7465 | return uc; | |
7466 | } | |
7467 | ||
af28855b | 7468 | /* Build the result of __builtin_offsetof. EXPR is a nested sequence of |
d897f7c2 | 7469 | component references, with STOP_REF, or alternatively an INDIRECT_REF of |
7470 | NULL, at the bottom; much like the traditional rendering of offsetof as a | |
7471 | macro. Returns the folded and properly cast result. */ | |
af28855b | 7472 | |
7473 | static tree | |
d897f7c2 | 7474 | fold_offsetof_1 (tree expr, tree stop_ref) |
af28855b | 7475 | { |
7476 | enum tree_code code = PLUS_EXPR; | |
7477 | tree base, off, t; | |
7478 | ||
d897f7c2 | 7479 | if (expr == stop_ref && TREE_CODE (expr) != ERROR_MARK) |
7480 | return size_zero_node; | |
7481 | ||
af28855b | 7482 | switch (TREE_CODE (expr)) |
7483 | { | |
7484 | case ERROR_MARK: | |
7485 | return expr; | |
7486 | ||
6b11d2e3 | 7487 | case VAR_DECL: |
7488 | error ("cannot apply %<offsetof%> to static data member %qD", expr); | |
7489 | return error_mark_node; | |
7490 | ||
d897f7c2 | 7491 | case CALL_EXPR: |
7492 | error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded"); | |
7493 | return error_mark_node; | |
7494 | ||
7495 | case INTEGER_CST: | |
7496 | gcc_assert (integer_zerop (expr)); | |
af28855b | 7497 | return size_zero_node; |
7498 | ||
d897f7c2 | 7499 | case NOP_EXPR: |
7500 | case INDIRECT_REF: | |
7501 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref); | |
7502 | gcc_assert (base == error_mark_node || base == size_zero_node); | |
7503 | return base; | |
7504 | ||
af28855b | 7505 | case COMPONENT_REF: |
d897f7c2 | 7506 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref); |
af28855b | 7507 | if (base == error_mark_node) |
7508 | return base; | |
7509 | ||
7510 | t = TREE_OPERAND (expr, 1); | |
7511 | if (DECL_C_BIT_FIELD (t)) | |
7512 | { | |
7513 | error ("attempt to take address of bit-field structure " | |
782858b8 | 7514 | "member %qD", t); |
af28855b | 7515 | return error_mark_node; |
7516 | } | |
7517 | off = size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (t), | |
7518 | size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t), 1) | |
7519 | / BITS_PER_UNIT)); | |
7520 | break; | |
7521 | ||
7522 | case ARRAY_REF: | |
d897f7c2 | 7523 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref); |
af28855b | 7524 | if (base == error_mark_node) |
7525 | return base; | |
7526 | ||
7527 | t = TREE_OPERAND (expr, 1); | |
7528 | if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0) | |
7529 | { | |
7530 | code = MINUS_EXPR; | |
49d00087 | 7531 | t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t); |
af28855b | 7532 | } |
7533 | t = convert (sizetype, t); | |
7534 | off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); | |
7535 | break; | |
7536 | ||
ede90cc2 | 7537 | case COMPOUND_EXPR: |
7538 | /* Handle static members of volatile structs. */ | |
7539 | t = TREE_OPERAND (expr, 1); | |
7540 | gcc_assert (TREE_CODE (t) == VAR_DECL); | |
7541 | return fold_offsetof_1 (t, stop_ref); | |
7542 | ||
af28855b | 7543 | default: |
231bd014 | 7544 | gcc_unreachable (); |
af28855b | 7545 | } |
7546 | ||
7547 | return size_binop (code, base, off); | |
7548 | } | |
7549 | ||
7550 | tree | |
d897f7c2 | 7551 | fold_offsetof (tree expr, tree stop_ref) |
af28855b | 7552 | { |
7553 | /* Convert back from the internal sizetype to size_t. */ | |
d897f7c2 | 7554 | return convert (size_type_node, fold_offsetof_1 (expr, stop_ref)); |
af28855b | 7555 | } |
7556 | ||
e35976b1 | 7557 | /* Print an error message for an invalid lvalue. USE says |
ab6bb714 | 7558 | how the lvalue is being used and so selects the error message. */ |
7559 | ||
e35976b1 | 7560 | void |
7561 | lvalue_error (enum lvalue_use use) | |
ab6bb714 | 7562 | { |
e35976b1 | 7563 | switch (use) |
ab6bb714 | 7564 | { |
e35976b1 | 7565 | case lv_assign: |
82d76a54 | 7566 | error ("lvalue required as left operand of assignment"); |
e35976b1 | 7567 | break; |
7568 | case lv_increment: | |
82d76a54 | 7569 | error ("lvalue required as increment operand"); |
e35976b1 | 7570 | break; |
7571 | case lv_decrement: | |
82d76a54 | 7572 | error ("lvalue required as decrement operand"); |
e35976b1 | 7573 | break; |
7574 | case lv_addressof: | |
82d76a54 | 7575 | error ("lvalue required as unary %<&%> operand"); |
e35976b1 | 7576 | break; |
7577 | case lv_asm: | |
82d76a54 | 7578 | error ("lvalue required in asm statement"); |
e35976b1 | 7579 | break; |
7580 | default: | |
7581 | gcc_unreachable (); | |
ab6bb714 | 7582 | } |
ab6bb714 | 7583 | } |
c271bdb2 | 7584 | \f |
7585 | /* *PTYPE is an incomplete array. Complete it with a domain based on | |
7586 | INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT | |
7587 | is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
7588 | 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ | |
7589 | ||
7590 | int | |
7591 | complete_array_type (tree *ptype, tree initial_value, bool do_default) | |
7592 | { | |
7593 | tree maxindex, type, main_type, elt, unqual_elt; | |
7594 | int failure = 0, quals; | |
6753bca0 | 7595 | hashval_t hashcode = 0; |
c271bdb2 | 7596 | |
7597 | maxindex = size_zero_node; | |
7598 | if (initial_value) | |
7599 | { | |
7600 | if (TREE_CODE (initial_value) == STRING_CST) | |
7601 | { | |
7602 | int eltsize | |
7603 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
7604 | maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1); | |
7605 | } | |
7606 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) | |
7607 | { | |
c75b4594 | 7608 | VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value); |
c271bdb2 | 7609 | |
c75b4594 | 7610 | if (VEC_empty (constructor_elt, v)) |
c271bdb2 | 7611 | { |
7612 | if (pedantic) | |
7613 | failure = 3; | |
7614 | maxindex = integer_minus_one_node; | |
7615 | } | |
7616 | else | |
7617 | { | |
7618 | tree curindex; | |
c75b4594 | 7619 | unsigned HOST_WIDE_INT cnt; |
7620 | constructor_elt *ce; | |
c271bdb2 | 7621 | |
c75b4594 | 7622 | if (VEC_index (constructor_elt, v, 0)->index) |
7623 | maxindex = fold_convert (sizetype, | |
7624 | VEC_index (constructor_elt, | |
7625 | v, 0)->index); | |
c271bdb2 | 7626 | curindex = maxindex; |
7627 | ||
c75b4594 | 7628 | for (cnt = 1; |
7629 | VEC_iterate (constructor_elt, v, cnt, ce); | |
7630 | cnt++) | |
c271bdb2 | 7631 | { |
c75b4594 | 7632 | if (ce->index) |
7633 | curindex = fold_convert (sizetype, ce->index); | |
c271bdb2 | 7634 | else |
7635 | curindex = size_binop (PLUS_EXPR, curindex, size_one_node); | |
7636 | ||
7637 | if (tree_int_cst_lt (maxindex, curindex)) | |
7638 | maxindex = curindex; | |
7639 | } | |
7640 | } | |
7641 | } | |
7642 | else | |
7643 | { | |
7644 | /* Make an error message unless that happened already. */ | |
7645 | if (initial_value != error_mark_node) | |
7646 | failure = 1; | |
7647 | } | |
7648 | } | |
7649 | else | |
7650 | { | |
7651 | failure = 2; | |
7652 | if (!do_default) | |
7653 | return failure; | |
7654 | } | |
7655 | ||
7656 | type = *ptype; | |
7657 | elt = TREE_TYPE (type); | |
7658 | quals = TYPE_QUALS (strip_array_types (elt)); | |
7659 | if (quals == 0) | |
7660 | unqual_elt = elt; | |
7661 | else | |
7662 | unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); | |
7663 | ||
7664 | /* Using build_distinct_type_copy and modifying things afterward instead | |
7665 | of using build_array_type to create a new type preserves all of the | |
7666 | TYPE_LANG_FLAG_? bits that the front end may have set. */ | |
7667 | main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); | |
7668 | TREE_TYPE (main_type) = unqual_elt; | |
7669 | TYPE_DOMAIN (main_type) = build_index_type (maxindex); | |
7670 | layout_type (main_type); | |
7671 | ||
6753bca0 | 7672 | /* Make sure we have the canonical MAIN_TYPE. */ |
7673 | hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode); | |
7674 | hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)), | |
7675 | hashcode); | |
7676 | main_type = type_hash_canon (hashcode, main_type); | |
7677 | ||
796735dc | 7678 | /* Fix the canonical type. */ |
7679 | if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type)) | |
7680 | || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type))) | |
7681 | SET_TYPE_STRUCTURAL_EQUALITY (main_type); | |
7682 | else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type) | |
7683 | || (TYPE_CANONICAL (TYPE_DOMAIN (main_type)) | |
7684 | != TYPE_DOMAIN (main_type))) | |
7685 | TYPE_CANONICAL (main_type) | |
7686 | = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)), | |
7687 | TYPE_CANONICAL (TYPE_DOMAIN (main_type))); | |
7688 | else | |
7689 | TYPE_CANONICAL (main_type) = main_type; | |
7690 | ||
c271bdb2 | 7691 | if (quals == 0) |
7692 | type = main_type; | |
7693 | else | |
7694 | type = c_build_qualified_type (main_type, quals); | |
7695 | ||
4f5b8f2a | 7696 | if (COMPLETE_TYPE_P (type) |
7697 | && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST | |
7698 | && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) | |
7699 | { | |
7700 | error ("size of array is too large"); | |
7701 | /* If we proceed with the array type as it is, we'll eventually | |
7702 | crash in tree_low_cst(). */ | |
7703 | type = error_mark_node; | |
7704 | } | |
7705 | ||
c271bdb2 | 7706 | *ptype = type; |
7707 | return failure; | |
7708 | } | |
ab6bb714 | 7709 | |
b6a5fc45 | 7710 | \f |
7711 | /* Used to help initialize the builtin-types.def table. When a type of | |
7712 | the correct size doesn't exist, use error_mark_node instead of NULL. | |
7713 | The later results in segfaults even when a decl using the type doesn't | |
7714 | get invoked. */ | |
7715 | ||
7716 | tree | |
7717 | builtin_type_for_size (int size, bool unsignedp) | |
7718 | { | |
7719 | tree type = lang_hooks.types.type_for_size (size, unsignedp); | |
7720 | return type ? type : error_mark_node; | |
7721 | } | |
7722 | ||
7723 | /* A helper function for resolve_overloaded_builtin in resolving the | |
7724 | overloaded __sync_ builtins. Returns a positive power of 2 if the | |
7725 | first operand of PARAMS is a pointer to a supported data type. | |
7726 | Returns 0 if an error is encountered. */ | |
7727 | ||
7728 | static int | |
7729 | sync_resolve_size (tree function, tree params) | |
7730 | { | |
7731 | tree type; | |
7732 | int size; | |
7733 | ||
7734 | if (params == NULL) | |
7735 | { | |
7736 | error ("too few arguments to function %qE", function); | |
7737 | return 0; | |
7738 | } | |
7739 | ||
7740 | type = TREE_TYPE (TREE_VALUE (params)); | |
7741 | if (TREE_CODE (type) != POINTER_TYPE) | |
7742 | goto incompatible; | |
7743 | ||
7744 | type = TREE_TYPE (type); | |
7745 | if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) | |
7746 | goto incompatible; | |
7747 | ||
7748 | size = tree_low_cst (TYPE_SIZE_UNIT (type), 1); | |
27213ba3 | 7749 | if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16) |
b6a5fc45 | 7750 | return size; |
7751 | ||
7752 | incompatible: | |
7753 | error ("incompatible type for argument %d of %qE", 1, function); | |
7754 | return 0; | |
7755 | } | |
7756 | ||
a0c938f0 | 7757 | /* A helper function for resolve_overloaded_builtin. Adds casts to |
b6a5fc45 | 7758 | PARAMS to make arguments match up with those of FUNCTION. Drops |
7759 | the variadic arguments at the end. Returns false if some error | |
7760 | was encountered; true on success. */ | |
7761 | ||
7762 | static bool | |
7763 | sync_resolve_params (tree orig_function, tree function, tree params) | |
7764 | { | |
7765 | tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (function)); | |
7766 | tree ptype; | |
7767 | int number; | |
7768 | ||
7769 | /* We've declared the implementation functions to use "volatile void *" | |
7770 | as the pointer parameter, so we shouldn't get any complaints from the | |
7771 | call to check_function_arguments what ever type the user used. */ | |
7772 | arg_types = TREE_CHAIN (arg_types); | |
7773 | ptype = TREE_TYPE (TREE_TYPE (TREE_VALUE (params))); | |
7774 | number = 2; | |
7775 | ||
7776 | /* For the rest of the values, we need to cast these to FTYPE, so that we | |
7777 | don't get warnings for passing pointer types, etc. */ | |
7778 | while (arg_types != void_list_node) | |
7779 | { | |
7780 | tree val; | |
7781 | ||
7782 | params = TREE_CHAIN (params); | |
7783 | if (params == NULL) | |
7784 | { | |
7785 | error ("too few arguments to function %qE", orig_function); | |
7786 | return false; | |
7787 | } | |
7788 | ||
7789 | /* ??? Ideally for the first conversion we'd use convert_for_assignment | |
7790 | so that we get warnings for anything that doesn't match the pointer | |
7791 | type. This isn't portable across the C and C++ front ends atm. */ | |
7792 | val = TREE_VALUE (params); | |
7793 | val = convert (ptype, val); | |
7794 | val = convert (TREE_VALUE (arg_types), val); | |
7795 | TREE_VALUE (params) = val; | |
7796 | ||
7797 | arg_types = TREE_CHAIN (arg_types); | |
7798 | number++; | |
7799 | } | |
7800 | ||
7801 | /* The definition of these primitives is variadic, with the remaining | |
7802 | being "an optional list of variables protected by the memory barrier". | |
7803 | No clue what that's supposed to mean, precisely, but we consider all | |
7804 | call-clobbered variables to be protected so we're safe. */ | |
7805 | TREE_CHAIN (params) = NULL; | |
7806 | ||
7807 | return true; | |
7808 | } | |
7809 | ||
a0c938f0 | 7810 | /* A helper function for resolve_overloaded_builtin. Adds a cast to |
b6a5fc45 | 7811 | RESULT to make it match the type of the first pointer argument in |
7812 | PARAMS. */ | |
7813 | ||
7814 | static tree | |
7815 | sync_resolve_return (tree params, tree result) | |
7816 | { | |
7817 | tree ptype = TREE_TYPE (TREE_TYPE (TREE_VALUE (params))); | |
10080eac | 7818 | ptype = TYPE_MAIN_VARIANT (ptype); |
b6a5fc45 | 7819 | return convert (ptype, result); |
7820 | } | |
7821 | ||
7822 | /* Some builtin functions are placeholders for other expressions. This | |
7823 | function should be called immediately after parsing the call expression | |
7824 | before surrounding code has committed to the type of the expression. | |
7825 | ||
7826 | FUNCTION is the DECL that has been invoked; it is known to be a builtin. | |
7827 | PARAMS is the argument list for the call. The return value is non-null | |
7828 | when expansion is complete, and null if normal processing should | |
7829 | continue. */ | |
7830 | ||
7831 | tree | |
7832 | resolve_overloaded_builtin (tree function, tree params) | |
7833 | { | |
7834 | enum built_in_function orig_code = DECL_FUNCTION_CODE (function); | |
65441f6f | 7835 | switch (DECL_BUILT_IN_CLASS (function)) |
7836 | { | |
7837 | case BUILT_IN_NORMAL: | |
7838 | break; | |
7839 | case BUILT_IN_MD: | |
7840 | if (targetm.resolve_overloaded_builtin) | |
a0c938f0 | 7841 | return targetm.resolve_overloaded_builtin (function, params); |
65441f6f | 7842 | else |
a0c938f0 | 7843 | return NULL_TREE; |
65441f6f | 7844 | default: |
7845 | return NULL_TREE; | |
7846 | } | |
a0c938f0 | 7847 | |
65441f6f | 7848 | /* Handle BUILT_IN_NORMAL here. */ |
b6a5fc45 | 7849 | switch (orig_code) |
7850 | { | |
7851 | case BUILT_IN_FETCH_AND_ADD_N: | |
7852 | case BUILT_IN_FETCH_AND_SUB_N: | |
7853 | case BUILT_IN_FETCH_AND_OR_N: | |
7854 | case BUILT_IN_FETCH_AND_AND_N: | |
7855 | case BUILT_IN_FETCH_AND_XOR_N: | |
7856 | case BUILT_IN_FETCH_AND_NAND_N: | |
7857 | case BUILT_IN_ADD_AND_FETCH_N: | |
7858 | case BUILT_IN_SUB_AND_FETCH_N: | |
7859 | case BUILT_IN_OR_AND_FETCH_N: | |
7860 | case BUILT_IN_AND_AND_FETCH_N: | |
7861 | case BUILT_IN_XOR_AND_FETCH_N: | |
7862 | case BUILT_IN_NAND_AND_FETCH_N: | |
7863 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_N: | |
7864 | case BUILT_IN_VAL_COMPARE_AND_SWAP_N: | |
7865 | case BUILT_IN_LOCK_TEST_AND_SET_N: | |
7866 | case BUILT_IN_LOCK_RELEASE_N: | |
7867 | { | |
7868 | int n = sync_resolve_size (function, params); | |
7869 | tree new_function, result; | |
7870 | ||
7871 | if (n == 0) | |
7872 | return error_mark_node; | |
7873 | ||
7874 | new_function = built_in_decls[orig_code + exact_log2 (n) + 1]; | |
7875 | if (!sync_resolve_params (function, new_function, params)) | |
7876 | return error_mark_node; | |
7877 | ||
7878 | result = build_function_call (new_function, params); | |
7879 | if (orig_code != BUILT_IN_BOOL_COMPARE_AND_SWAP_N | |
7880 | && orig_code != BUILT_IN_LOCK_RELEASE_N) | |
7881 | result = sync_resolve_return (params, result); | |
7882 | ||
7883 | return result; | |
7884 | } | |
7885 | ||
7886 | default: | |
65441f6f | 7887 | return NULL_TREE; |
b6a5fc45 | 7888 | } |
7889 | } | |
7890 | ||
491255f5 | 7891 | /* Ignoring their sign, return true if two scalar types are the same. */ |
7892 | bool | |
7893 | same_scalar_type_ignoring_signedness (tree t1, tree t2) | |
7894 | { | |
7895 | enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); | |
7896 | ||
9421ebb9 | 7897 | gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE) |
7898 | && (c2 == INTEGER_TYPE || c2 == REAL_TYPE | |
7899 | || c2 == FIXED_POINT_TYPE)); | |
491255f5 | 7900 | |
7901 | /* Equality works here because c_common_signed_type uses | |
7902 | TYPE_MAIN_VARIANT. */ | |
11773141 | 7903 | return c_common_signed_type (t1) |
7904 | == c_common_signed_type (t2); | |
491255f5 | 7905 | } |
7906 | ||
be7350e7 | 7907 | /* Check for missing format attributes on function pointers. LTYPE is |
7908 | the new type or left-hand side type. RTYPE is the old type or | |
7909 | right-hand side type. Returns TRUE if LTYPE is missing the desired | |
7910 | attribute. */ | |
7911 | ||
7912 | bool | |
7913 | check_missing_format_attribute (tree ltype, tree rtype) | |
7914 | { | |
7915 | tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype); | |
7916 | tree ra; | |
7917 | ||
7918 | for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra)) | |
7919 | if (is_attribute_p ("format", TREE_PURPOSE (ra))) | |
7920 | break; | |
7921 | if (ra) | |
7922 | { | |
7923 | tree la; | |
7924 | for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la)) | |
7925 | if (is_attribute_p ("format", TREE_PURPOSE (la))) | |
7926 | break; | |
7927 | return !la; | |
7928 | } | |
7929 | else | |
7930 | return false; | |
7931 | } | |
7932 | ||
2840aae4 | 7933 | /* Subscripting with type char is likely to lose on a machine where |
7934 | chars are signed. So warn on any machine, but optionally. Don't | |
7935 | warn for unsigned char since that type is safe. Don't warn for | |
7936 | signed char because anyone who uses that must have done so | |
7937 | deliberately. Furthermore, we reduce the false positive load by | |
7938 | warning only for non-constant value of type char. */ | |
7939 | ||
7940 | void | |
7941 | warn_array_subscript_with_type_char (tree index) | |
7942 | { | |
7943 | if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node | |
7944 | && TREE_CODE (index) != INTEGER_CST) | |
7945 | warning (OPT_Wchar_subscripts, "array subscript has type %<char%>"); | |
7946 | } | |
7947 | ||
e534436e | 7948 | /* Implement -Wparentheses for the unexpected C precedence rules, to |
7949 | cover cases like x + y << z which readers are likely to | |
7950 | misinterpret. We have seen an expression in which CODE is a binary | |
7951 | operator used to combine expressions headed by CODE_LEFT and | |
7952 | CODE_RIGHT. CODE_LEFT and CODE_RIGHT may be ERROR_MARK, which | |
7953 | means that that side of the expression was not formed using a | |
7954 | binary operator, or it was enclosed in parentheses. */ | |
7955 | ||
7956 | void | |
7957 | warn_about_parentheses (enum tree_code code, enum tree_code code_left, | |
7958 | enum tree_code code_right) | |
7959 | { | |
7960 | if (!warn_parentheses) | |
7961 | return; | |
7962 | ||
7963 | if (code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
7964 | { | |
7965 | if (code_left == PLUS_EXPR || code_left == MINUS_EXPR | |
7966 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
7967 | warning (OPT_Wparentheses, | |
7968 | "suggest parentheses around + or - inside shift"); | |
7969 | } | |
7970 | ||
7971 | if (code == TRUTH_ORIF_EXPR) | |
7972 | { | |
7973 | if (code_left == TRUTH_ANDIF_EXPR | |
7974 | || code_right == TRUTH_ANDIF_EXPR) | |
7975 | warning (OPT_Wparentheses, | |
7976 | "suggest parentheses around && within ||"); | |
7977 | } | |
7978 | ||
7979 | if (code == BIT_IOR_EXPR) | |
7980 | { | |
7981 | if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR | |
7982 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR | |
7983 | || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR | |
7984 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
7985 | warning (OPT_Wparentheses, | |
7986 | "suggest parentheses around arithmetic in operand of |"); | |
7987 | /* Check cases like x|y==z */ | |
7988 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
7989 | || TREE_CODE_CLASS (code_right) == tcc_comparison) | |
7990 | warning (OPT_Wparentheses, | |
7991 | "suggest parentheses around comparison in operand of |"); | |
7992 | } | |
7993 | ||
7994 | if (code == BIT_XOR_EXPR) | |
7995 | { | |
7996 | if (code_left == BIT_AND_EXPR | |
7997 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR | |
7998 | || code_right == BIT_AND_EXPR | |
7999 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
8000 | warning (OPT_Wparentheses, | |
8001 | "suggest parentheses around arithmetic in operand of ^"); | |
8002 | /* Check cases like x^y==z */ | |
8003 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
8004 | || TREE_CODE_CLASS (code_right) == tcc_comparison) | |
8005 | warning (OPT_Wparentheses, | |
8006 | "suggest parentheses around comparison in operand of ^"); | |
8007 | } | |
8008 | ||
8009 | if (code == BIT_AND_EXPR) | |
8010 | { | |
8011 | if (code_left == PLUS_EXPR || code_left == MINUS_EXPR | |
8012 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
8013 | warning (OPT_Wparentheses, | |
8014 | "suggest parentheses around + or - in operand of &"); | |
8015 | /* Check cases like x&y==z */ | |
8016 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
8017 | || TREE_CODE_CLASS (code_right) == tcc_comparison) | |
8018 | warning (OPT_Wparentheses, | |
8019 | "suggest parentheses around comparison in operand of &"); | |
8020 | } | |
8021 | ||
6ce0c450 | 8022 | if (code == EQ_EXPR || code == NE_EXPR) |
8023 | { | |
8024 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
8025 | || TREE_CODE_CLASS (code_right) == tcc_comparison) | |
8026 | warning (OPT_Wparentheses, | |
8027 | "suggest parentheses around comparison in operand of %s", | |
8028 | code == EQ_EXPR ? "==" : "!="); | |
8029 | } | |
8030 | else if (TREE_CODE_CLASS (code) == tcc_comparison) | |
8031 | { | |
8032 | if ((TREE_CODE_CLASS (code_left) == tcc_comparison | |
8033 | && code_left != NE_EXPR && code_left != EQ_EXPR) | |
8034 | || (TREE_CODE_CLASS (code_right) == tcc_comparison | |
8035 | && code_right != NE_EXPR && code_right != EQ_EXPR)) | |
8036 | warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not " | |
8037 | "have their mathematical meaning"); | |
8038 | } | |
e534436e | 8039 | } |
8040 | ||
92fccaaa | 8041 | /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ |
8042 | ||
8043 | void | |
8044 | warn_for_unused_label (tree label) | |
8045 | { | |
8046 | if (!TREE_USED (label)) | |
8047 | { | |
8048 | if (DECL_INITIAL (label)) | |
8049 | warning (OPT_Wunused_label, "label %q+D defined but not used", label); | |
8050 | else | |
8051 | warning (OPT_Wunused_label, "label %q+D declared but not defined", label); | |
8052 | } | |
8053 | } | |
2840aae4 | 8054 | |
b9fc964a | 8055 | #ifndef TARGET_HAS_TARGETCM |
8056 | struct gcc_targetcm targetcm = TARGETCM_INITIALIZER; | |
8057 | #endif | |
8058 | ||
f092582b | 8059 | /* Warn for division by zero according to the value of DIVISOR. */ |
8060 | ||
8061 | void | |
8062 | warn_for_div_by_zero (tree divisor) | |
8063 | { | |
9421ebb9 | 8064 | /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning |
8065 | about division by zero. Do not issue a warning if DIVISOR has a | |
f092582b | 8066 | floating-point type, since we consider 0.0/0.0 a valid way of |
8067 | generating a NaN. */ | |
9421ebb9 | 8068 | if (skip_evaluation == 0 |
8069 | && (integer_zerop (divisor) || fixed_zerop (divisor))) | |
f092582b | 8070 | warning (OPT_Wdiv_by_zero, "division by zero"); |
8071 | } | |
8072 | ||
1f3233d1 | 8073 | #include "gt-c-common.h" |