OSDN Git Service

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