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