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