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