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