1 /* Definitions for C parsing and type checking.
2 Copyright (C) 1987, 1993, 1994, 1995, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
27 #include "diagnostic.h"
29 /* struct lang_identifier is private to c-decl.c, but langhooks.c needs to
30 know how big it is. This is sanity-checked in c-decl.c. */
31 #define C_SIZEOF_STRUCT_LANG_IDENTIFIER \
32 (sizeof (struct c_common_identifier) + 3 * sizeof (void *))
34 /* In a RECORD_TYPE or UNION_TYPE, nonzero if any component is read-only. */
35 #define C_TYPE_FIELDS_READONLY(TYPE) TREE_LANG_FLAG_1 (TYPE)
37 /* In a RECORD_TYPE or UNION_TYPE, nonzero if any component is volatile. */
38 #define C_TYPE_FIELDS_VOLATILE(TYPE) TREE_LANG_FLAG_2 (TYPE)
40 /* In a RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE
41 nonzero if the definition of the type has already started. */
42 #define C_TYPE_BEING_DEFINED(TYPE) TYPE_LANG_FLAG_0 (TYPE)
44 /* In an incomplete RECORD_TYPE or UNION_TYPE, a list of variable
45 declarations whose type would be completed by completing that type. */
46 #define C_TYPE_INCOMPLETE_VARS(TYPE) TYPE_VFIELD (TYPE)
48 /* In an IDENTIFIER_NODE, nonzero if this identifier is actually a
49 keyword. C_RID_CODE (node) is then the RID_* value of the keyword,
50 and C_RID_YYCODE is the token number wanted by Yacc. */
51 #define C_IS_RESERVED_WORD(ID) TREE_LANG_FLAG_0 (ID)
53 /* Record whether a type or decl was written with nonconstant size.
54 Note that TYPE_SIZE may have simplified to a constant. */
55 #define C_TYPE_VARIABLE_SIZE(TYPE) TYPE_LANG_FLAG_1 (TYPE)
56 #define C_DECL_VARIABLE_SIZE(TYPE) DECL_LANG_FLAG_0 (TYPE)
58 /* Record whether a type is defined inside a struct or union type.
59 This is used for -Wc++-compat. */
60 #define C_TYPE_DEFINED_IN_STRUCT(TYPE) TYPE_LANG_FLAG_2 (TYPE)
62 /* Record whether a typedef for type `int' was actually `signed int'. */
63 #define C_TYPEDEF_EXPLICITLY_SIGNED(EXP) DECL_LANG_FLAG_1 (EXP)
65 /* For a FUNCTION_DECL, nonzero if it was defined without an explicit
67 #define C_FUNCTION_IMPLICIT_INT(EXP) DECL_LANG_FLAG_1 (EXP)
69 /* For a FUNCTION_DECL, nonzero if it was an implicit declaration. */
70 #define C_DECL_IMPLICIT(EXP) DECL_LANG_FLAG_2 (EXP)
72 /* For FUNCTION_DECLs, evaluates true if the decl is built-in but has
74 #define C_DECL_DECLARED_BUILTIN(EXP) \
75 DECL_LANG_FLAG_3 (FUNCTION_DECL_CHECK (EXP))
77 /* For FUNCTION_DECLs, evaluates true if the decl is built-in, has a
78 built-in prototype and does not have a non-built-in prototype. */
79 #define C_DECL_BUILTIN_PROTOTYPE(EXP) \
80 DECL_LANG_FLAG_6 (FUNCTION_DECL_CHECK (EXP))
82 /* Record whether a decl was declared register. This is strictly a
83 front-end flag, whereas DECL_REGISTER is used for code generation;
84 they may differ for structures with volatile fields. */
85 #define C_DECL_REGISTER(EXP) DECL_LANG_FLAG_4 (EXP)
87 /* Record whether a decl was used in an expression anywhere except an
88 unevaluated operand of sizeof / typeof / alignof. This is only
89 used for functions declared static but not defined, though outside
90 sizeof and typeof it is set for other function decls as well. */
91 #define C_DECL_USED(EXP) DECL_LANG_FLAG_5 (FUNCTION_DECL_CHECK (EXP))
93 /* Record whether a variable has been declared threadprivate by
94 #pragma omp threadprivate. */
95 #define C_DECL_THREADPRIVATE_P(DECL) DECL_LANG_FLAG_3 (VAR_DECL_CHECK (DECL))
97 /* Nonzero for a decl which either doesn't exist or isn't a prototype.
98 N.B. Could be simplified if all built-in decls had complete prototypes
99 (but this is presently difficult because some of them need FILE*). */
100 #define C_DECL_ISNT_PROTOTYPE(EXP) \
102 || (TYPE_ARG_TYPES (TREE_TYPE (EXP)) == 0 \
103 && !DECL_BUILT_IN (EXP)))
105 /* For FUNCTION_TYPE, a hidden list of types of arguments. The same as
106 TYPE_ARG_TYPES for functions with prototypes, but created for functions
107 without prototypes. */
108 #define TYPE_ACTUAL_ARG_TYPES(NODE) TYPE_LANG_SLOT_1 (NODE)
110 /* For a CONSTRUCTOR, whether some initializer contains a
111 subexpression meaning it is not a constant expression. */
112 #define CONSTRUCTOR_NON_CONST(EXPR) TREE_LANG_FLAG_1 (CONSTRUCTOR_CHECK (EXPR))
114 /* Record parser information about an expression that is irrelevant
115 for code generation alongside a tree representing its value. */
118 /* The value of the expression. */
120 /* Record the original unary/binary operator of an expression, which may
121 have been changed by fold, STRING_CST for unparenthesized string
122 constants, C_MAYBE_CONST_EXPR for __builtin_constant_p calls
123 (even if parenthesized), for subexpressions, and for non-constant
124 initializers, or ERROR_MARK for other expressions (including
125 parenthesized expressions). */
126 enum tree_code original_code;
127 /* If not NULL, the original type of an expression. This will
128 differ from the type of the value field for an enum constant.
129 The type of an enum constant is a plain integer type, but this
130 field will be the enum type. */
134 /* A kind of type specifier. Note that this information is currently
135 only used to distinguish tag definitions, tag references and typeof
137 enum c_typespec_kind {
138 /* A reserved keyword type specifier. */
140 /* A reference to a tag, previously declared, such as "struct foo".
141 This includes where the previous declaration was as a different
142 kind of tag, in which case this is only valid if shadowing that
143 tag in an inner scope. */
145 /* A reference to a tag, not previously declared in a visible
148 /* A definition of a tag such as "struct foo { int a; }". */
150 /* A typedef name. */
152 /* An ObjC-specific kind of type specifier. */
154 /* A typeof specifier. */
158 /* A type specifier: this structure is created in the parser and
159 passed to declspecs_add_type only. */
161 /* What kind of type specifier this is. */
162 enum c_typespec_kind kind;
163 /* The specifier itself. */
165 /* An expression to be evaluated before the type specifier, in the
166 case of typeof specifiers, or NULL otherwise or if no such
167 expression is required for a particular typeof specifier. In
168 particular, when typeof is applied to an expression of variably
169 modified type, that expression must be evaluated in order to
170 determine array sizes that form part of the type, but the
171 expression itself (as opposed to the array sizes) forms no part
172 of the type and so needs to be recorded separately. */
174 /* Whether the expression has operands suitable for use in constant
176 bool expr_const_operands;
179 /* A storage class specifier. */
180 enum c_storage_class {
189 /* A type specifier keyword "void", "_Bool", "char", "int", "float",
190 "double", "_Decimal32", "_Decimal64", "_Decimal128", "_Fract", "_Accum",
192 enum c_typespec_keyword {
207 /* A sequence of declaration specifiers in C. */
209 /* The type specified, if a single type specifier such as a struct,
210 union or enum specifier, typedef name or typeof specifies the
211 whole type, or NULL_TREE if none or a keyword such as "void" or
212 "char" is used. Does not include qualifiers. */
214 /* Any expression to be evaluated before the type, from a typeof
217 /* The attributes from a typedef decl. */
219 /* When parsing, the attributes. Outside the parser, this will be
220 NULL; attributes (possibly from multiple lists) will be passed
223 /* Any type specifier keyword used such as "int", not reflecting
224 modifiers such as "short", or cts_none if none. */
225 enum c_typespec_keyword typespec_word;
226 /* The storage class specifier, or csc_none if none. */
227 enum c_storage_class storage_class;
228 /* Whether any expressions in typeof specifiers may appear in
229 constant expressions. */
230 BOOL_BITFIELD expr_const_operands : 1;
231 /* Whether any declaration specifiers have been seen at all. */
232 BOOL_BITFIELD declspecs_seen_p : 1;
233 /* Whether a type specifier has been seen. */
234 BOOL_BITFIELD type_seen_p : 1;
235 /* Whether something other than a storage class specifier or
236 attribute has been seen. This is used to warn for the
237 obsolescent usage of storage class specifiers other than at the
238 start of the list. (Doing this properly would require function
239 specifiers to be handled separately from storage class
241 BOOL_BITFIELD non_sc_seen_p : 1;
242 /* Whether the type is specified by a typedef or typeof name. */
243 BOOL_BITFIELD typedef_p : 1;
244 /* Whether a struct, union or enum type either had its content
245 defined by a type specifier in the list or was the first visible
246 declaration of its tag. */
247 BOOL_BITFIELD tag_defined_p : 1;
248 /* Whether the type is explicitly "signed" or specified by a typedef
249 whose type is explicitly "signed". */
250 BOOL_BITFIELD explicit_signed_p : 1;
251 /* Whether the specifiers include a deprecated typedef. */
252 BOOL_BITFIELD deprecated_p : 1;
253 /* Whether the type defaulted to "int" because there were no type
255 BOOL_BITFIELD default_int_p;
256 /* Whether "long" was specified. */
257 BOOL_BITFIELD long_p : 1;
258 /* Whether "long" was specified more than once. */
259 BOOL_BITFIELD long_long_p : 1;
260 /* Whether "short" was specified. */
261 BOOL_BITFIELD short_p : 1;
262 /* Whether "signed" was specified. */
263 BOOL_BITFIELD signed_p : 1;
264 /* Whether "unsigned" was specified. */
265 BOOL_BITFIELD unsigned_p : 1;
266 /* Whether "complex" was specified. */
267 BOOL_BITFIELD complex_p : 1;
268 /* Whether "inline" was specified. */
269 BOOL_BITFIELD inline_p : 1;
270 /* Whether "__thread" was specified. */
271 BOOL_BITFIELD thread_p : 1;
272 /* Whether "const" was specified. */
273 BOOL_BITFIELD const_p : 1;
274 /* Whether "volatile" was specified. */
275 BOOL_BITFIELD volatile_p : 1;
276 /* Whether "restrict" was specified. */
277 BOOL_BITFIELD restrict_p : 1;
278 /* Whether "_Sat" was specified. */
279 BOOL_BITFIELD saturating_p : 1;
282 /* The various kinds of declarators in C. */
283 enum c_declarator_kind {
292 /* Parenthesized declarator with nested attributes. */
296 /* Information about the parameters in a function declarator. */
298 /* A list of parameter decls. */
300 /* A list of structure, union and enum tags defined. */
302 /* A list of argument types to go in the FUNCTION_TYPE. */
304 /* A list of non-parameter decls (notably enumeration constants)
305 defined with the parameters. */
307 /* A list of VLA sizes from the parameters. In a function
308 definition, these are used to ensure that side-effects in sizes
309 of arrays converted to pointers (such as a parameter int i[n++])
310 take place; otherwise, they are ignored. */
312 /* True when these arguments had [*]. */
313 BOOL_BITFIELD had_vla_unspec : 1;
317 struct c_declarator {
318 /* The kind of declarator. */
319 enum c_declarator_kind kind;
320 /* Except for cdk_id, the contained declarator. For cdk_id, NULL. */
321 struct c_declarator *declarator;
322 location_t id_loc; /* Currently only set for cdk_id, cdk_array. */
324 /* For identifiers, an IDENTIFIER_NODE or NULL_TREE if an abstract
328 struct c_arg_info *arg_info;
331 /* The array dimension, or NULL for [] and [*]. */
333 /* The qualifiers inside []. */
335 /* The attributes (currently ignored) inside []. */
337 /* Whether [static] was used. */
338 BOOL_BITFIELD static_p : 1;
339 /* Whether [*] was used. */
340 BOOL_BITFIELD vla_unspec_p : 1;
342 /* For pointers, the qualifiers on the pointer type. */
344 /* For attributes. */
351 /* The declaration specifiers. */
352 struct c_declspecs *specs;
353 /* The declarator. */
354 struct c_declarator *declarator;
359 /* The declaration specifiers, minus any prefix attributes. */
360 struct c_declspecs *specs;
361 /* The attributes. */
363 /* The declarator. */
364 struct c_declarator *declarator;
367 /* Used when parsing an enum. Initialized by start_enum. */
368 struct c_enum_contents
370 /* While defining an enum type, this is 1 plus the last enumerator
372 tree enum_next_value;
374 /* Nonzero means that there was overflow computing enum_next_value. */
378 /* A type of reference to a static identifier in an inline
380 enum c_inline_static_type {
381 /* Identifier with internal linkage used in function that may be an
382 inline definition (i.e., file-scope static). */
384 /* Modifiable object with static storage duration defined in
385 function that may be an inline definition (i.e., local
392 extern void c_parse_init (void);
394 /* in c-aux-info.c */
395 extern void gen_aux_info_record (tree, int, int, int);
398 struct c_spot_bindings;
399 struct c_struct_parse_info;
400 extern struct obstack parser_obstack;
401 extern tree c_break_label;
402 extern tree c_cont_label;
404 extern int global_bindings_p (void);
405 extern void push_scope (void);
406 extern tree pop_scope (void);
407 extern void c_bindings_start_stmt_expr (struct c_spot_bindings *);
408 extern void c_bindings_end_stmt_expr (struct c_spot_bindings *);
410 extern void record_inline_static (location_t, tree, tree,
411 enum c_inline_static_type);
412 extern void c_init_decl_processing (void);
413 extern void c_print_identifier (FILE *, tree, int);
414 extern int quals_from_declspecs (const struct c_declspecs *);
415 extern struct c_declarator *build_array_declarator (location_t, tree,
416 struct c_declspecs *,
418 extern tree build_enumerator (location_t, struct c_enum_contents *, tree, tree);
419 extern tree check_for_loop_decls (location_t);
420 extern void mark_forward_parm_decls (void);
421 extern void declare_parm_level (void);
422 extern void undeclared_variable (location_t, tree);
423 extern tree lookup_label_for_goto (location_t, tree);
424 extern tree declare_label (tree);
425 extern tree define_label (location_t, tree);
426 extern struct c_spot_bindings *c_get_switch_bindings (void);
427 extern void c_release_switch_bindings (struct c_spot_bindings *);
428 extern bool c_check_switch_jump_warnings (struct c_spot_bindings *,
429 location_t, location_t);
430 extern void finish_decl (tree, location_t, tree, tree, tree);
431 extern tree finish_enum (tree, tree, tree);
432 extern void finish_function (void);
433 extern tree finish_struct (location_t, tree, tree, tree,
434 struct c_struct_parse_info *);
435 extern struct c_arg_info *get_parm_info (bool);
436 extern tree grokfield (location_t, struct c_declarator *,
437 struct c_declspecs *, tree, tree *);
438 extern tree groktypename (struct c_type_name *, tree *, bool *);
439 extern tree grokparm (const struct c_parm *);
440 extern tree implicitly_declare (location_t, tree);
441 extern void keep_next_level (void);
442 extern void pending_xref_error (void);
443 extern void c_push_function_context (void);
444 extern void c_pop_function_context (void);
445 extern void push_parm_decl (const struct c_parm *);
446 extern struct c_declarator *set_array_declarator_inner (struct c_declarator *,
447 struct c_declarator *);
448 extern tree c_builtin_function (tree);
449 extern tree c_builtin_function_ext_scope (tree);
450 extern void shadow_tag (const struct c_declspecs *);
451 extern void shadow_tag_warned (const struct c_declspecs *, int);
452 extern tree start_enum (location_t, struct c_enum_contents *, tree);
453 extern int start_function (struct c_declspecs *, struct c_declarator *, tree);
454 extern tree start_decl (struct c_declarator *, struct c_declspecs *, bool,
456 extern tree start_struct (location_t, enum tree_code, tree,
457 struct c_struct_parse_info **);
458 extern void store_parm_decls (void);
459 extern void store_parm_decls_from (struct c_arg_info *);
460 extern tree xref_tag (enum tree_code, tree);
461 extern struct c_typespec parser_xref_tag (location_t, enum tree_code, tree);
462 extern int c_expand_decl (tree);
463 extern struct c_parm *build_c_parm (struct c_declspecs *, tree,
464 struct c_declarator *);
465 extern struct c_declarator *build_attrs_declarator (tree,
466 struct c_declarator *);
467 extern struct c_declarator *build_function_declarator (struct c_arg_info *,
468 struct c_declarator *);
469 extern struct c_declarator *build_id_declarator (tree);
470 extern struct c_declarator *make_pointer_declarator (struct c_declspecs *,
471 struct c_declarator *);
472 extern struct c_declspecs *build_null_declspecs (void);
473 extern struct c_declspecs *declspecs_add_qual (struct c_declspecs *, tree);
474 extern struct c_declspecs *declspecs_add_type (location_t,
475 struct c_declspecs *,
477 extern struct c_declspecs *declspecs_add_scspec (struct c_declspecs *, tree);
478 extern struct c_declspecs *declspecs_add_attrs (struct c_declspecs *, tree);
479 extern struct c_declspecs *finish_declspecs (struct c_declspecs *);
481 /* in c-objc-common.c */
482 extern bool c_objc_common_init (void);
483 extern bool c_missing_noreturn_ok_p (tree);
484 extern bool c_warn_unused_global_decl (const_tree);
485 extern void c_initialize_diagnostics (diagnostic_context *);
486 extern bool c_vla_unspec_p (tree x, tree fn);
488 #define c_build_type_variant(TYPE, CONST_P, VOLATILE_P) \
489 c_build_qualified_type ((TYPE), \
490 ((CONST_P) ? TYPE_QUAL_CONST : 0) | \
491 ((VOLATILE_P) ? TYPE_QUAL_VOLATILE : 0))
494 extern bool in_late_binary_op;
495 extern int in_alignof;
496 extern int in_sizeof;
497 extern int in_typeof;
499 extern struct c_switch *c_switch_stack;
501 extern tree c_objc_common_truthvalue_conversion (location_t, tree);
502 extern tree require_complete_type (tree);
503 extern int same_translation_unit_p (const_tree, const_tree);
504 extern int comptypes (tree, tree);
505 extern bool c_vla_type_p (const_tree);
506 extern bool c_mark_addressable (tree);
507 extern void c_incomplete_type_error (const_tree, const_tree);
508 extern tree c_type_promotes_to (tree);
509 extern struct c_expr default_function_array_conversion (location_t,
511 extern tree composite_type (tree, tree);
512 extern tree build_component_ref (location_t, tree, tree);
513 extern tree build_array_ref (location_t, tree, tree);
514 extern tree build_external_ref (location_t, tree, int, tree *);
515 extern void pop_maybe_used (bool);
516 extern struct c_expr c_expr_sizeof_expr (location_t, struct c_expr);
517 extern struct c_expr c_expr_sizeof_type (location_t, struct c_type_name *);
518 extern struct c_expr parser_build_unary_op (location_t, enum tree_code,
520 extern struct c_expr parser_build_binary_op (location_t,
521 enum tree_code, struct c_expr,
523 extern tree build_conditional_expr (location_t, tree, bool, tree, tree,
525 extern tree build_compound_expr (location_t, tree, tree);
526 extern tree c_cast_expr (location_t, struct c_type_name *, tree);
527 extern tree build_c_cast (location_t, tree, tree);
528 extern void store_init_value (location_t, tree, tree, tree);
529 extern void error_init (const char *);
530 extern void pedwarn_init (location_t, int opt, const char *);
531 extern void maybe_warn_string_init (tree, struct c_expr);
532 extern void start_init (tree, tree, int);
533 extern void finish_init (void);
534 extern void really_start_incremental_init (tree);
535 extern void push_init_level (int);
536 extern struct c_expr pop_init_level (int);
537 extern void set_init_index (tree, tree);
538 extern void set_init_label (tree);
539 extern void process_init_element (struct c_expr, bool);
540 extern tree build_compound_literal (location_t, tree, tree, bool);
541 extern void check_compound_literal_type (location_t, struct c_type_name *);
542 extern tree c_start_case (location_t, location_t, tree);
543 extern void c_finish_case (tree);
544 extern tree build_asm_expr (location_t, tree, tree, tree, tree, tree, bool);
545 extern tree build_asm_stmt (tree, tree);
546 extern int c_types_compatible_p (tree, tree);
547 extern tree c_begin_compound_stmt (bool);
548 extern tree c_end_compound_stmt (location_t, tree, bool);
549 extern void c_finish_if_stmt (location_t, tree, tree, tree, bool);
550 extern void c_finish_loop (location_t, tree, tree, tree, tree, tree, bool);
551 extern tree c_begin_stmt_expr (void);
552 extern tree c_finish_stmt_expr (location_t, tree);
553 extern tree c_process_expr_stmt (location_t, tree);
554 extern tree c_finish_expr_stmt (location_t, tree);
555 extern tree c_finish_return (location_t, tree, tree);
556 extern tree c_finish_bc_stmt (location_t, tree *, bool);
557 extern tree c_finish_goto_label (location_t, tree);
558 extern tree c_finish_goto_ptr (location_t, tree);
559 extern tree c_expr_to_decl (tree, bool *, bool *);
560 extern tree c_begin_omp_parallel (void);
561 extern tree c_finish_omp_parallel (location_t, tree, tree);
562 extern tree c_begin_omp_task (void);
563 extern tree c_finish_omp_task (location_t, tree, tree);
564 extern tree c_finish_omp_clauses (tree);
565 extern tree c_build_va_arg (location_t, tree, tree);
567 /* Set to 0 at beginning of a function definition, set to 1 if
568 a return statement that specifies a return value is seen. */
570 extern int current_function_returns_value;
572 /* Set to 0 at beginning of a function definition, set to 1 if
573 a return statement with no argument is seen. */
575 extern int current_function_returns_null;
577 /* Set to 0 at beginning of a function definition, set to 1 if
578 a call to a noreturn function is seen. */
580 extern int current_function_returns_abnormally;
582 /* Nonzero means we are reading code that came from a system header file. */
584 extern int system_header_p;
586 /* True means global_bindings_p should return false even if the scope stack
587 says we are in file scope. */
589 extern bool c_override_global_bindings_to_false;
592 extern void c_finish_incomplete_decl (tree);
593 extern void c_write_global_declarations (void);
595 /* In order for the format checking to accept the C frontend
596 diagnostic framework extensions, you must include this file before
597 toplev.h, not after. */
598 #if GCC_VERSION >= 4001
599 #define ATTRIBUTE_GCC_CDIAG(m, n) __attribute__ ((__format__ (GCC_DIAG_STYLE, m ,n))) ATTRIBUTE_NONNULL(m)
601 #define ATTRIBUTE_GCC_CDIAG(m, n) ATTRIBUTE_NONNULL(m)
604 extern void pedwarn_c90 (location_t, int opt, const char *, ...) ATTRIBUTE_GCC_CDIAG(3,4);
605 extern void pedwarn_c99 (location_t, int opt, const char *, ...) ATTRIBUTE_GCC_CDIAG(3,4);
607 extern bool c_cpp_error (cpp_reader *, int, location_t, unsigned int,
608 const char *, va_list *)
609 ATTRIBUTE_GCC_CDIAG(5,0);
611 #endif /* ! GCC_C_TREE_H */