%{
#include <sys/cdefs.h>
#if defined(__RCSID)
__RCSID("$NetBSD: cgram.y,v 1.538 2026/03/18 06:17:55 rillig Exp $");
#endif
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "lint1.h"
extern char *yytext;
int block_level;
size_t mem_block_level;
#define LWARN_NOTHING_SAVED (-3)
static int saved_lwarn = LWARN_NOTHING_SAVED;
static void cgram_declare(sym_t *, bool, sbuf_t *);
static void read_until_rparen(void);
static balanced_token_sequence read_balanced_token_sequence(void);
static sym_t *symbolrename(sym_t *, sbuf_t *);
static void
clear_warning_flags_loc(const char *file, size_t line)
{
debug_step("%s:%zu: clearing flags", file, line);
reset_suppressions();
saved_lwarn = LWARN_NOTHING_SAVED;
}
static void
save_warning_flags_loc(const char *file, size_t line)
{
debug_step("%s:%zu: saving flags %d", file, line, lwarn);
saved_lwarn = lwarn;
}
static void
restore_warning_flags_loc(const char *file, size_t line)
{
if (saved_lwarn != LWARN_NOTHING_SAVED) {
lwarn = saved_lwarn;
debug_step("%s:%zu: restoring flags %d", file, line, lwarn);
} else
clear_warning_flags_loc(file, line);
}
#define clear_warning_flags() clear_warning_flags_loc(__FILE__, __LINE__)
#define save_warning_flags() save_warning_flags_loc(__FILE__, __LINE__)
#define restore_warning_flags() restore_warning_flags_loc(__FILE__, __LINE__)
static bool
is_either(const char *s, const char *a, const char *b)
{
return strcmp(s, a) == 0 || strcmp(s, b) == 0;
}
static void
attribute_list_add(attribute_list *list, attribute attr)
{
if (list->len >= list->cap) {
attribute *old_attrs = list->attrs;
list->cap = 16 + 2 * list->cap;
list->attrs = block_zero_alloc(
list->cap * sizeof(*list->attrs), "attribute[]");
if (list->len > 0)
memcpy(list->attrs, old_attrs,
list->len * sizeof(*list->attrs));
}
list->attrs[list->len++] = attr;
}
static void
attribute_list_add_all(attribute_list *dst, attribute_list src)
{
for (size_t i = 0, n = src.len; i < n; i++)
attribute_list_add(dst, src.attrs[i]);
}
static attribute
new_attribute(const sbuf_t *prefix, const sbuf_t *name,
const balanced_token_sequence *arg)
{
attribute attr = { .name = xstrdup(name->sb_name) };
if (prefix != NULL)
attr.prefix = xstrdup(prefix->sb_name);
if (arg != NULL) {
attr.arg = block_zero_alloc(sizeof(*attr.arg),
"balanced_token_sequence");
*attr.arg = *arg;
}
return attr;
}
static tnode_t *
unconst_tnode(const tnode_t *p)
{
void *r;
memcpy(&r, &p, sizeof(r));
return r;
}
#if YYDEBUG && YYBYACC
#define YYSTYPE_TOSTRING cgram_to_string
#endif
%}
%expect 110
%union {
val_t *y_val;
sbuf_t *y_name;
sym_t *y_sym;
bool y_inc;
op_t y_op;
scl_t y_scl;
tspec_t y_tspec;
type_qualifiers y_type_qualifiers;
type_attributes y_type_attributes;
function_specifier y_function_specifier;
parameter_list y_parameter_list;
function_call *y_arguments;
type_t *y_type;
tnode_t *y_tnode;
range_t y_range;
buffer *y_string;
qual_ptr *y_qual_ptr;
bool y_seen_statement;
struct generic_selection y_generic_selection;
array_size y_array_size;
bool y_in_system_header;
designation y_designation;
named_constant y_named_constant;
attribute y_attribute;
attribute_list y_attribute_list;
balanced_token_sequence y_tokens;
};
%token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPAREN T_RPAREN
%token T_POINT T_ARROW
%token T_COMPLEMENT T_LOGNOT
%token <y_inc> T_INCDEC
%token T_SIZEOF
%token T_BUILTIN_OFFSETOF
%token T_TYPEOF
%token T_EXTENSION
%token T_ALIGNAS
%token T_ALIGNOF
%token T_ASTERISK
%token <y_op> T_MULTIPLICATIVE
%token <y_op> T_ADDITIVE
%token <y_op> T_SHIFT
%token <y_op> T_RELATIONAL
%token <y_op> T_EQUALITY
%token T_AMPER
%token T_BITXOR
%token T_BITOR
%token T_LOGAND
%token T_LOGOR
%token T_QUEST
%token T_COLON
%token T_ASSIGN
%token <y_op> T_OPASSIGN
%token T_COMMA
%token T_SEMI
%token T_ELLIPSIS
%token T_DCOLON
%token T_REAL
%token T_IMAG
%token T_GENERIC
%token <y_scl> T_SCLASS
%token <y_function_specifier> T_FUNCTION_SPECIFIER
%token <y_tspec> T_TYPE
%token <y_type_qualifiers> T_QUAL
%token <y_type_qualifiers> T_ATOMIC
%token <y_tspec> T_STRUCT_OR_UNION
%token T_ASM
%token T_BREAK
%token T_CASE
%token T_CONTINUE
%token T_DEFAULT
%token T_DO
%token T_ELSE
%token T_ENUM
%token T_FOR
%token T_GOTO
%token T_IF
%token T_PACKED
%token T_RETURN
%token T_STATIC_ASSERT
%token T_SWITCH
%token T_SYMBOLRENAME
%token T_WHILE
%token T_ATTRIBUTE
%left T_THEN
%left T_ELSE
%right T_QUEST T_COLON
%left T_LOGOR
%left T_LOGAND
%left T_BITOR
%left T_BITXOR
%left T_AMPER
%left T_EQUALITY
%left T_RELATIONAL
%left T_SHIFT
%left T_ADDITIVE
%left T_ASTERISK T_MULTIPLICATIVE
%token <y_name> T_NAME
%token <y_name> T_TYPENAME
%token <y_val> T_CON
%token <y_named_constant> T_NAMED_CONSTANT
%token <y_string> T_STRING
%type <y_sym> identifier_sym
%type <y_name> identifier
%type <y_string> string
%type <y_tnode> primary_expression
%type <y_designation> member_designator
%type <y_tnode> generic_selection
%type <y_generic_selection> generic_inner
%type <y_tnode> postfix_expression
%type <y_tnode> gcc_statement_expr_list
%type <y_tnode> gcc_statement_expr_item
%type <y_op> point_or_arrow
%type <y_arguments> argument_expression_list
%type <y_scl> storage_class_specifiers
%type <y_tnode> unary_expression
%type <y_tnode> sizeof_argument
%type <y_tnode> cast_expression
%type <y_tnode> expression_opt
%type <y_tnode> conditional_expression
%type <y_tnode> assignment_expression
%type <y_tnode> expression
%type <y_tnode> constant_expression
%type <y_type> begin_type_typespec
%type <y_type_attributes> type_attribute_list_opt
%type <y_type_attributes> type_attribute_list
%type <y_type_attributes> type_attribute
%type <y_scl> storage_class_specifier
%type <y_type> type_type_specifier
%type <y_type> notype_type_specifier
%type <y_type> struct_or_union_specifier
%type <y_tspec> struct_or_union
%type <y_sym> braced_member_declaration_list
%type <y_sym> member_declaration_list_with_rbrace
%type <y_sym> member_declaration_list
%type <y_sym> member_declaration
%type <y_sym> notype_member_declarator_list
%type <y_sym> type_member_declarator_list
%type <y_sym> notype_member_declarator
%type <y_sym> type_member_declarator
%type <y_type> enum_specifier
%type <y_sym> enum_declaration
%type <y_sym> enums_with_opt_comma
%type <y_sym> enumerator_list
%type <y_sym> enumerator
%type <y_type> atomic_type_specifier
%type <y_type_qualifiers> type_qualifier
%type <y_sym> notype_declarator
%type <y_sym> type_declarator
%type <y_sym> notype_direct_declarator
%type <y_sym> type_direct_declarator
%type <y_qual_ptr> pointer
%type <y_type_qualifiers> type_qualifier_list_opt
%type <y_type_qualifiers> type_qualifier_list
%type <y_sym> parameter_declaration
%type <y_sym> type_param_declarator
%type <y_sym> notype_param_declarator
%type <y_sym> direct_param_declarator
%type <y_sym> direct_notype_param_declarator
%type <y_parameter_list> param_list
%type <y_array_size> array_size_opt
%type <y_sym> identifier_list
%type <y_type> type_name
%type <y_sym> abstract_declaration
%type <y_parameter_list> abstract_decl_param_list
%type <y_parameter_list> vararg_parameter_type_list
%type <y_parameter_list> parameter_type_list
%type <y_sym> abstract_declarator
%type <y_sym> direct_abstract_declarator
%type <y_range> range
%type <y_attribute_list> attribute_specifier_sequence
%type <y_attribute_list> attribute_specifier
%type <y_attribute_list> attribute_list
%type <y_attribute> attribute
%type <y_tokens> attribute_argument_clause
%type <y_name> asm_or_symbolrename_opt
%type <y_seen_statement> block_item_list
%type <y_seen_statement> block_item
%type <y_sym> func_declarator
%type <y_type_attributes> gcc_attribute_specifier_list_opt
%type <y_type_attributes> gcc_attribute_specifier_list
%type <y_type_attributes> gcc_attribute_specifier
%type <y_type_attributes> gcc_attribute_list
%type <y_type_attributes> gcc_attribute
%type <y_in_system_header> sys
%%
program:
{
if (!allow_trad && !allow_c99)
error(272);
else if (allow_c90)
warning(272);
}
| translation_unit
;
identifier_sym:
identifier {
$$ = getsym($1);
}
;
identifier:
T_NAME {
debug_step("cgram: name '%s'", $1->sb_name);
$$ = $1;
}
| T_TYPENAME {
debug_step("cgram: typename '%s'", $1->sb_name);
$$ = $1;
}
;
string:
T_STRING
| string T_STRING {
if (!allow_c90)
warning(219);
$$ = cat_strings($1, $2);
}
;
primary_expression:
T_NAME {
bool sys_name, sys_next;
sys_name = in_system_header;
if (yychar < 0)
yychar = yylex();
sys_next = in_system_header;
in_system_header = sys_name;
$$ = build_name(getsym($1), yychar == T_LPAREN);
in_system_header = sys_next;
}
| T_CON {
$$ = build_constant(gettyp($1->v_tspec), $1);
}
| T_NAMED_CONSTANT {
if ($1 == NC_NULLPTR) {
tnode_t *zero = expr_alloc_tnode();
zero->tn_op = CON;
zero->tn_type = gettyp(INT);
zero->u.value.v_tspec = INT;
type_t *void_ptr = block_derive_type(gettyp(VOID), PTR);
$$ = convert(CVT, 0, void_ptr, zero);
$$->tn_sys = zero->tn_sys;
} else {
tnode_t *nc = expr_alloc_tnode();
nc->tn_op = CON;
nc->tn_type = gettyp(BOOL);
nc->u.value.v_tspec = BOOL;
nc->u.value.u.integer = $1 == NC_TRUE ? 1 : 0;
$$ = nc;
}
}
| string {
$$ = build_string($1);
}
| T_LPAREN expression T_RPAREN {
if ($2 != NULL)
$2->tn_parenthesized = true;
$$ = $2;
}
| generic_selection
| T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA {
set_sym_kind(SK_MEMBER);
} member_designator T_RPAREN {
$$ = build_offsetof($3, $6);
}
;
member_designator:
identifier {
$$ = (designation) { .dn_len = 0 };
designation_push(&$$, DK_MEMBER, getsym($1), 0);
}
| member_designator T_LBRACK range T_RBRACK {
$$ = $1;
designation_push(&$$, DK_SUBSCRIPT, NULL, $3.lo);
}
| member_designator T_POINT {
set_sym_kind(SK_MEMBER);
} identifier {
$$ = $1;
designation_push(&$$, DK_MEMBER, getsym($4), 0);
}
;
generic_selection:
T_GENERIC {
c11ism(345);
} T_LPAREN generic_inner T_RPAREN {
$$ = $4.matched != NULL ? $4.matched : $4.fallback;
}
;
generic_inner:
{
push_evaluation_mode(EM_TYPE);
} assignment_expression {
pop_evaluation_mode();
} {
$$ = (struct generic_selection){
.expr_type = $2 != NULL
? cconv(unconst_tnode($2))->tn_type
: NULL
};
}
| generic_inner T_COMMA type_name T_COLON {
$1.assoc_matched = $1.expr_type != NULL && $3 != NULL
&& types_compatible($1.expr_type, $3, true, false, NULL);
push_evaluation_mode($1.assoc_matched ? EM_EVAL : EM_PARSE);
} assignment_expression {
pop_evaluation_mode();
$$ = $1;
if ($$.assoc_matched)
$$.matched = $6;
}
| generic_inner T_COMMA T_DEFAULT T_COLON {
push_evaluation_mode($1.matched != NULL ? EM_PARSE : EM_EVAL);
} assignment_expression {
pop_evaluation_mode();
$$ = $1;
$$.fallback = $6;
}
;
postfix_expression:
primary_expression
| postfix_expression T_LBRACK sys expression T_RBRACK {
$$ = build_unary(INDIR, $3, build_binary($1, PLUS, $3, $4));
}
| postfix_expression T_LPAREN sys T_RPAREN {
function_call *call =
expr_zero_alloc(sizeof(*call), "function_call");
$$ = build_function_call($1, $3, call);
}
| postfix_expression T_LPAREN sys argument_expression_list T_RPAREN {
$$ = build_function_call($1, $3, $4);
}
| postfix_expression point_or_arrow sys T_NAME {
$$ = build_member_access($1, $2, $3, $4);
}
| postfix_expression T_INCDEC sys {
$$ = build_unary($2 ? INCAFT : DECAFT, $3, $1);
}
| T_LPAREN type_name T_RPAREN {
sym_t *tmp = mktempsym($2);
begin_initialization(tmp);
cgram_declare(tmp, true, NULL);
} braced_initializer {
if (!allow_c99)
gnuism(319);
$$ = build_name(current_initsym(), false);
end_initialization();
}
| T_LPAREN storage_class_specifiers type_name T_RPAREN {
sym_t *tmp = mktempsym($3);
tmp->s_scl = $2;
begin_initialization(tmp);
cgram_declare(tmp, true, NULL);
} braced_initializer {
if (!allow_c99)
gnuism(319);
$$ = build_name(current_initsym(), false);
end_initialization();
}
| T_LPAREN compound_statement_lbrace {
begin_statement_expr();
} gcc_statement_expr_list {
do_statement_expr($4);
} compound_statement_rbrace T_RPAREN {
$$ = end_statement_expr();
}
;
gcc_statement_expr_list:
gcc_statement_expr_item
| gcc_statement_expr_list gcc_statement_expr_item {
$$ = $2;
}
;
gcc_statement_expr_item:
declaration_or_error {
clear_warning_flags();
$$ = NULL;
}
| non_expr_statement {
$$ = expr_alloc_tnode();
$$->tn_type = gettyp(VOID);
}
| T_SEMI {
$$ = expr_alloc_tnode();
$$->tn_type = gettyp(VOID);
}
| expression T_SEMI {
if ($1 == NULL) {
$$ = expr_alloc_tnode();
$$->tn_type = gettyp(VOID);
} else {
if ($1->tn_op == NAME)
$1->u.sym->s_used = true;
expr($1, true, false, false, false,
"statement expression");
suppress_fallthrough = false;
$$ = $1;
}
}
;
point_or_arrow:
T_POINT {
set_sym_kind(SK_MEMBER);
$$ = POINT;
}
| T_ARROW {
set_sym_kind(SK_MEMBER);
$$ = ARROW;
}
;
argument_expression_list:
assignment_expression {
$$ = expr_zero_alloc(sizeof(*$$), "function_call");
add_function_argument($$, $1);
}
| argument_expression_list T_COMMA assignment_expression {
$$ = $1;
add_function_argument($1, $3);
}
;
storage_class_specifiers:
storage_class_specifier
| storage_class_specifiers storage_class_specifier {
$$ = $1;
}
;
unary_expression:
postfix_expression
| T_INCDEC sys unary_expression {
$$ = build_unary($1 ? INCBEF : DECBEF, $2, $3);
}
| T_AMPER sys cast_expression {
$$ = build_unary(ADDR, $2, $3);
}
| T_ASTERISK sys cast_expression {
$$ = build_unary(INDIR, $2, $3);
}
| T_ADDITIVE sys cast_expression {
if (!allow_c90 && $1 == PLUS)
warning(100);
$$ = build_unary($1 == PLUS ? UPLUS : UMINUS, $2, $3);
}
| T_COMPLEMENT sys cast_expression {
$$ = build_unary(COMPL, $2, $3);
}
| T_LOGNOT sys cast_expression {
$$ = build_unary(NOT, $2, $3);
}
| T_REAL sys cast_expression {
$$ = build_unary(REAL, $2, $3);
}
| T_IMAG sys cast_expression {
$$ = build_unary(IMAG, $2, $3);
}
| T_EXTENSION cast_expression {
$$ = $2;
}
| T_SIZEOF {
push_evaluation_mode(EM_TYPE);
} sizeof_argument {
pop_evaluation_mode();
$$ = $3;
}
| T_ALIGNOF unary_expression {
gnuism(349);
lint_assert($2 != NULL);
$$ = build_alignof($2->tn_type);
}
| T_ALIGNOF T_LPAREN type_name T_RPAREN {
$$ = build_alignof($3);
}
;
sizeof_argument:
unary_expression {
$$ = $1 == NULL ? NULL : build_sizeof($1->tn_type);
if ($$ != NULL)
check_expr_misc($1,
false, false, false, false, false, true);
}
| T_LPAREN type_name T_RPAREN {
$$ = build_sizeof($2);
}
;
cast_expression:
unary_expression
| T_LPAREN type_name T_RPAREN sys cast_expression {
$$ = cast($5, $4, $2);
}
;
expression_opt:
{
$$ = NULL;
}
| expression
;
conditional_expression:
cast_expression
| conditional_expression T_ASTERISK sys conditional_expression {
$$ = build_binary($1, MULT, $3, $4);
}
| conditional_expression T_MULTIPLICATIVE sys conditional_expression {
$$ = build_binary($1, $2, $3, $4);
}
| conditional_expression T_ADDITIVE sys conditional_expression {
$$ = build_binary($1, $2, $3, $4);
}
| conditional_expression T_SHIFT sys conditional_expression {
$$ = build_binary($1, $2, $3, $4);
}
| conditional_expression T_RELATIONAL sys conditional_expression {
$$ = build_binary($1, $2, $3, $4);
}
| conditional_expression T_EQUALITY sys conditional_expression {
$$ = build_binary($1, $2, $3, $4);
}
| conditional_expression T_AMPER sys conditional_expression {
$$ = build_binary($1, BITAND, $3, $4);
}
| conditional_expression T_BITXOR sys conditional_expression {
$$ = build_binary($1, BITXOR, $3, $4);
}
| conditional_expression T_BITOR sys conditional_expression {
$$ = build_binary($1, BITOR, $3, $4);
}
| conditional_expression T_LOGAND sys conditional_expression {
$$ = build_binary($1, LOGAND, $3, $4);
}
| conditional_expression T_LOGOR sys conditional_expression {
$$ = build_binary($1, LOGOR, $3, $4);
}
| conditional_expression T_QUEST sys
expression T_COLON sys conditional_expression {
$$ = build_binary($1, QUEST, $3,
build_binary($4, COLON, $6, $7));
}
;
assignment_expression:
conditional_expression
| unary_expression T_ASSIGN sys assignment_expression {
$$ = build_binary($1, ASSIGN, $3, $4);
}
| unary_expression T_OPASSIGN sys assignment_expression {
$$ = build_binary($1, $2, $3, $4);
}
;
expression:
assignment_expression
| expression T_COMMA sys assignment_expression {
$$ = build_binary($1, COMMA, $3, $4);
}
;
constant_expression:
conditional_expression
;
declaration_or_error:
declaration
| error T_SEMI
;
declaration:
begin_type_declmods end_type T_SEMI {
if (dcs->d_scl == TYPEDEF)
warning(72);
else
warning(2);
}
| begin_type_declmods end_type notype_init_declarator_list T_SEMI {
if (dcs->d_scl == TYPEDEF)
error(249, "missing base type for typedef");
else
error(1);
}
| begin_type_declaration_specifiers end_type T_SEMI {
if (dcs->d_scl == TYPEDEF)
warning(72);
else if (!dcs->d_nonempty_decl)
warning(2);
}
| begin_type_declaration_specifiers end_type
type_init_declarator_list T_SEMI
| static_assert_declaration
;
begin_type_declaration_specifiers:
begin_type_typespec {
dcs_add_type($1);
}
| begin_type_declmods type_type_specifier {
dcs_add_type($2);
}
| type_attribute begin_type_declaration_specifiers {
dcs_add_type_attributes($1);
}
| begin_type_declaration_specifiers declmod
| begin_type_declaration_specifiers notype_type_specifier {
dcs_add_type($2);
}
;
begin_type_declmods:
begin_type type_qualifier {
dcs_add_qualifiers($2);
}
| begin_type T_SCLASS {
dcs_add_storage_class($2);
}
| begin_type T_FUNCTION_SPECIFIER {
dcs_add_function_specifier($2);
}
| begin_type_declmods declmod
;
begin_type_specifier_qualifier_list:
begin_type_specifier_qualifier_list_postfix
| type_attribute_list begin_type_specifier_qualifier_list_postfix
;
begin_type_specifier_qualifier_list_postfix:
begin_type_typespec {
dcs_add_type($1);
}
| begin_type_qualifier_list type_type_specifier {
dcs_add_type($2);
}
| begin_type_specifier_qualifier_list_postfix type_qualifier {
dcs_add_qualifiers($2);
}
| begin_type_specifier_qualifier_list_postfix notype_type_specifier {
dcs_add_type($2);
}
| begin_type_specifier_qualifier_list_postfix type_attribute
;
begin_type_typespec:
begin_type notype_type_specifier {
$$ = $2;
}
| begin_type T_TYPENAME {
$$ = getsym($2)->s_type;
}
;
begin_type_qualifier_list:
begin_type type_qualifier {
dcs_add_qualifiers($2);
}
| begin_type_qualifier_list type_qualifier {
dcs_add_qualifiers($2);
}
;
declmod:
type_qualifier {
dcs_add_qualifiers($1);
}
| T_SCLASS {
dcs_add_storage_class($1);
}
| T_FUNCTION_SPECIFIER {
dcs_add_function_specifier($1);
}
| type_attribute_list {
dcs_add_type_attributes($1);
}
;
type_attribute_list_opt:
{
$$ = (type_attributes){ .used = false };
}
| type_attribute_list
;
type_attribute_list:
type_attribute
| type_attribute_list type_attribute {
$$ = merge_type_attributes($1, $2);
}
;
type_attribute:
gcc_attribute_specifier
| T_ALIGNAS T_LPAREN type_type_specifier T_RPAREN {
dcs_add_alignas(alignment($3));
$$ = no_type_attributes();
}
| T_ALIGNAS T_LPAREN constant_expression T_RPAREN {
dcs_add_alignas(to_int_constant($3, true));
$$ = no_type_attributes();
}
| T_PACKED {
dcs_add_packed();
$$ = no_type_attributes();
}
;
begin_type:
{
dcs_begin_type();
}
| attribute_specifier_sequence {
dcs_begin_type();
dcs->d_used = attributes_contain(&$1, "maybe_unused");
dcs->d_noreturn = attributes_contain(&$1, "noreturn");
}
;
end_type:
{
dcs_end_type();
}
;
notype_init_declarator_list:
notype_init_declarator
| notype_init_declarator_list T_COMMA type_init_declarator
;
type_init_declarator_list:
type_init_declarator
| type_init_declarator_list T_COMMA type_init_declarator
;
notype_init_declarator:
notype_declarator asm_or_symbolrename_opt {
cgram_declare($1, false, $2);
check_size($1);
}
| notype_declarator asm_or_symbolrename_opt {
begin_initialization($1);
cgram_declare($1, true, $2);
} T_ASSIGN initializer {
check_size($1);
end_initialization();
}
;
type_init_declarator:
type_declarator asm_or_symbolrename_opt {
cgram_declare($1, false, $2);
check_size($1);
}
| type_declarator asm_or_symbolrename_opt {
begin_initialization($1);
cgram_declare($1, true, $2);
} T_ASSIGN initializer {
if ($1->s_type->t_tspec != AUTO_TYPE)
check_size($1);
end_initialization();
}
;
storage_class_specifier:
T_SCLASS
;
type_type_specifier:
notype_type_specifier
| T_TYPENAME {
$$ = getsym($1)->s_type;
}
;
notype_type_specifier:
T_TYPE {
$$ = gettyp($1);
}
| T_TYPEOF T_LPAREN expression T_RPAREN {
$$ = $3 != NULL ? block_dup_type($3->tn_type) : gettyp(INT);
$$->t_typeof = true;
}
| atomic_type_specifier
| struct_or_union_specifier {
end_declaration_level();
$$ = $1;
}
| enum_specifier {
end_declaration_level();
$$ = $1;
}
;
struct_or_union_specifier:
struct_or_union identifier_sym {
$$ = make_tag_type($2, $1, false, yychar == T_SEMI);
}
| struct_or_union identifier_sym {
dcs->d_tag_type = make_tag_type($2, $1, true, false);
} braced_member_declaration_list {
$$ = complete_struct_or_union($4);
}
| struct_or_union {
dcs->d_tag_type = make_tag_type(NULL, $1, true, false);
} braced_member_declaration_list {
$$ = complete_struct_or_union($3);
}
| struct_or_union error {
set_sym_kind(SK_VCFT);
$$ = gettyp(INT);
}
;
struct_or_union:
T_STRUCT_OR_UNION {
set_sym_kind(SK_TAG);
begin_declaration_level($1 == STRUCT ? DLK_STRUCT : DLK_UNION);
dcs->d_sou_size_in_bits = 0;
dcs->d_sou_align = 1;
$$ = $1;
}
| struct_or_union type_attribute
;
braced_member_declaration_list:
T_LBRACE {
set_sym_kind(SK_VCFT);
} member_declaration_list_with_rbrace {
$$ = $3;
}
;
member_declaration_list_with_rbrace:
member_declaration_list T_RBRACE
| T_RBRACE {
$$ = NULL;
}
;
member_declaration_list:
member_declaration
| member_declaration_list member_declaration {
$$ = concat_symbols($1, $2);
}
;
member_declaration:
begin_type_qualifier_list end_type {
set_sym_kind(SK_MEMBER);
} notype_member_declarator_list T_SEMI {
set_sym_kind(SK_VCFT);
$$ = $4;
}
| begin_type_specifier_qualifier_list end_type {
set_sym_kind(SK_MEMBER);
} type_member_declarator_list T_SEMI {
set_sym_kind(SK_VCFT);
$$ = $4;
}
| begin_type_qualifier_list end_type type_attribute_list_opt T_SEMI {
error(249, "member without type");
$$ = NULL;
}
| begin_type_specifier_qualifier_list end_type T_SEMI {
set_sym_kind(SK_VCFT);
if (!allow_c11 && !allow_gcc)
warning(49);
if (is_struct_or_union(dcs->d_type->t_tspec))
$$ = declare_unnamed_member();
else {
error(249, "unnamed member");
$$ = NULL;
}
}
| static_assert_declaration {
$$ = NULL;
}
| error T_SEMI {
set_sym_kind(SK_VCFT);
$$ = NULL;
}
;
notype_member_declarator_list:
notype_member_declarator {
$$ = declare_member($1);
}
| notype_member_declarator_list {
set_sym_kind(SK_MEMBER);
} T_COMMA type_member_declarator {
$$ = concat_symbols($1, declare_member($4));
}
;
type_member_declarator_list:
type_member_declarator {
$$ = declare_member($1);
}
| type_member_declarator_list {
set_sym_kind(SK_MEMBER);
} T_COMMA type_member_declarator {
$$ = concat_symbols($1, declare_member($4));
}
;
notype_member_declarator:
notype_declarator
| notype_declarator T_COLON constant_expression {
$$ = set_bit_field_width($1, to_int_constant($3, true));
}
| {
set_sym_kind(SK_VCFT);
} T_COLON constant_expression {
$$ = set_bit_field_width(NULL, to_int_constant($3, true));
}
;
type_member_declarator:
type_declarator
| type_declarator T_COLON constant_expression type_attribute_list_opt {
$$ = set_bit_field_width($1, to_int_constant($3, true));
}
| {
set_sym_kind(SK_VCFT);
} T_COLON constant_expression type_attribute_list_opt {
$$ = set_bit_field_width(NULL, to_int_constant($3, true));
}
;
enum_specifier:
enum gcc_attribute_specifier_list_opt identifier_sym {
$$ = make_tag_type($3, ENUM, false, false);
}
| enum gcc_attribute_specifier_list_opt identifier_sym {
dcs->d_tag_type = make_tag_type($3, ENUM, true, false);
} enum_declaration {
$$ = complete_enum($5);
}
| enum gcc_attribute_specifier_list_opt {
dcs->d_tag_type = make_tag_type(NULL, ENUM, true, false);
} enum_declaration {
$$ = complete_enum($4);
}
| enum error {
set_sym_kind(SK_VCFT);
$$ = gettyp(INT);
}
;
enum:
T_ENUM {
set_sym_kind(SK_TAG);
begin_declaration_level(DLK_ENUM);
}
;
enum_declaration:
T_LBRACE {
set_sym_kind(SK_VCFT);
enumval = 0;
} enums_with_opt_comma T_RBRACE {
$$ = $3;
}
;
enums_with_opt_comma:
enumerator_list
| enumerator_list T_COMMA {
if (!allow_c99 && !allow_trad)
error(54);
else
c99ism(54);
$$ = $1;
}
;
enumerator_list:
enumerator
| enumerator_list T_COMMA enumerator {
$$ = concat_symbols($1, $3);
}
| error {
$$ = NULL;
}
;
enumerator:
identifier_sym gcc_attribute_specifier_list_opt {
$$ = enumeration_constant($1, enumval, true);
}
| identifier_sym gcc_attribute_specifier_list_opt
T_ASSIGN constant_expression {
$$ = enumeration_constant($1, to_int_constant($4, true),
false);
}
;
atomic_type_specifier:
atomic T_LPAREN type_name T_RPAREN {
$$ = $3;
}
;
atomic:
T_ATOMIC {
if (!allow_c11)
error(350);
}
;
type_qualifier:
T_QUAL
| atomic {
$$ = (type_qualifiers){ .tq_atomic = true };
}
;
notype_declarator:
notype_direct_declarator
| pointer notype_direct_declarator {
$$ = add_pointer($2, $1);
}
;
type_declarator:
type_direct_declarator
| pointer type_direct_declarator {
$$ = add_pointer($2, $1);
}
;
notype_direct_declarator:
type_attribute_list_opt T_NAME {
$$ = declarator_name(getsym($2));
}
| type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
$$ = $3;
}
| notype_direct_declarator T_LBRACK array_size_opt T_RBRACK {
$$ = add_array($1, $3.has_dim, $3.dim);
}
| notype_direct_declarator param_list asm_or_symbolrename_opt {
$$ = add_function(symbolrename($1, $3), $2);
end_declaration_level();
block_level--;
}
| notype_direct_declarator type_attribute
;
type_direct_declarator:
type_attribute_list_opt identifier {
$$ = declarator_name(getsym($2));
}
| type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
$$ = $3;
}
| type_direct_declarator T_LBRACK array_size_opt T_RBRACK {
$$ = add_array($1, $3.has_dim, $3.dim);
}
| type_direct_declarator param_list asm_or_symbolrename_opt {
$$ = add_function(symbolrename($1, $3), $2);
end_declaration_level();
block_level--;
if ($2.used)
$$->s_used = true;
}
| type_direct_declarator type_attribute {
$$ = $1;
if ($2.used)
$$->s_used = true;
if ($2.bit_width > 0) {
tspec_t t = $$->s_type->t_tspec;
lint_assert(t == INT || t == UINT);
type_t *tp = block_dup_type($$->s_type);
tp->t_tspec =
$2.bit_width == 128 ? (t == INT ? INT128 : UINT128) :
t == INT ? LLONG : ULLONG;
$$->s_type = tp;
}
}
;
pointer:
T_ASTERISK type_qualifier_list_opt {
$$ = xcalloc(1, sizeof(*$$));
add_type_qualifiers(&$$->qualifiers, $2);
}
| T_ASTERISK type_qualifier_list_opt pointer {
$$ = xcalloc(1, sizeof(*$$));
add_type_qualifiers(&$$->qualifiers, $2);
$$ = append_qualified_pointer($$, $3);
}
;
type_qualifier_list_opt:
{
$$ = (type_qualifiers){ .tq_const = false };
}
| type_qualifier_list
;
type_qualifier_list:
type_qualifier
| type_qualifier_list type_qualifier {
$$ = $1;
add_type_qualifiers(&$$, $2);
}
;
parameter_declaration:
begin_type_declmods end_type {
$$ = declare_parameter(abstract_name(), false);
}
| begin_type_declaration_specifiers end_type {
$$ = declare_parameter(abstract_name(), false);
}
| begin_type_declmods end_type notype_param_declarator {
$$ = declare_parameter($3, false);
}
| begin_type_declaration_specifiers end_type type_param_declarator {
$$ = declare_parameter($3, false);
}
| begin_type_declmods end_type abstract_declarator {
$$ = declare_parameter($3, false);
}
| begin_type_declaration_specifiers end_type abstract_declarator {
$$ = declare_parameter($3, false);
}
;
type_param_declarator:
direct_param_declarator
| pointer direct_param_declarator {
$$ = add_pointer($2, $1);
}
;
notype_param_declarator:
direct_notype_param_declarator
| pointer direct_notype_param_declarator {
$$ = add_pointer($2, $1);
}
;
direct_param_declarator:
identifier type_attribute_list {
$$ = declarator_name(getsym($1));
if ($2.used)
dcs_set_used();
}
| identifier {
$$ = declarator_name(getsym($1));
}
| T_LPAREN notype_param_declarator T_RPAREN {
$$ = $2;
}
| direct_param_declarator T_LBRACK array_size_opt T_RBRACK
gcc_attribute_specifier_list_opt {
$$ = add_array($1, $3.has_dim, $3.dim);
if ($5.used)
dcs_set_used();
}
| direct_param_declarator param_list asm_or_symbolrename_opt {
$$ = add_function(symbolrename($1, $3), $2);
end_declaration_level();
block_level--;
if ($2.used)
dcs_set_used();
}
;
direct_notype_param_declarator:
identifier {
$$ = declarator_name(getsym($1));
}
| T_LPAREN notype_param_declarator T_RPAREN {
$$ = $2;
}
| direct_notype_param_declarator T_LBRACK array_size_opt T_RBRACK {
$$ = add_array($1, $3.has_dim, $3.dim);
}
| direct_notype_param_declarator param_list asm_or_symbolrename_opt {
$$ = add_function(symbolrename($1, $3), $2);
end_declaration_level();
block_level--;
}
;
param_list:
T_LPAREN {
block_level++;
begin_declaration_level(DLK_PROTO_PARAMS);
} identifier_list T_RPAREN {
$$ = (parameter_list){ .first = $3, .identifier = true };
}
| abstract_decl_param_list
;
array_size_opt:
{
$$.has_dim = false;
$$.dim = 0;
}
| T_ASTERISK {
$$.has_dim = false;
$$.dim = 0;
}
| type_qualifier_list_opt T_SCLASS constant_expression {
if ($2 != STATIC)
yyerror("Bad attribute");
c11ism(343);
$$.has_dim = true;
$$.dim = $3 == NULL ? 0 : to_int_constant($3, false);
}
| type_qualifier {
if (!$1.tq_restrict)
yyerror("Bad attribute");
$$.has_dim = true;
$$.dim = 0;
}
| constant_expression {
$$.has_dim = true;
$$.dim = $1 == NULL ? 0 : to_int_constant($1, false);
}
;
identifier_list:
T_NAME {
$$ = old_style_function_parameter_name(getsym($1));
}
| identifier_list T_COMMA T_NAME {
$$ = concat_symbols($1,
old_style_function_parameter_name(getsym($3)));
}
| identifier_list error
;
type_name:
{
begin_declaration_level(DLK_ABSTRACT);
} abstract_declaration {
end_declaration_level();
$$ = $2->s_type;
}
;
abstract_declaration:
begin_type_qualifier_list end_type {
$$ = declare_abstract_type(abstract_name());
}
| begin_type_specifier_qualifier_list end_type {
$$ = declare_abstract_type(abstract_name());
}
| begin_type_qualifier_list end_type abstract_declarator {
$$ = declare_abstract_type($3);
}
| begin_type_specifier_qualifier_list end_type abstract_declarator {
$$ = declare_abstract_type($3);
}
;
abstract_decl_param_list:
abstract_decl_lparen T_RPAREN type_attribute_list_opt {
$$ = (parameter_list){ .used = $3.used };
}
| abstract_decl_lparen vararg_parameter_type_list T_RPAREN
type_attribute_list_opt {
$$ = $2;
$$.prototype = true;
$$.used = $4.used;
$$.noreturn = $4.noreturn;
}
| abstract_decl_lparen error T_RPAREN type_attribute_list_opt {
$$ = (parameter_list){ .used = $4.used };
}
;
abstract_decl_lparen:
T_LPAREN {
block_level++;
begin_declaration_level(DLK_PROTO_PARAMS);
}
;
vararg_parameter_type_list:
parameter_type_list
| parameter_type_list T_COMMA T_ELLIPSIS {
$$ = $1;
$$.vararg = true;
}
| T_ELLIPSIS {
if (!allow_trad && !allow_c99)
error(84);
else if (allow_c90)
warning(84);
$$ = (parameter_list){ .vararg = true };
}
;
parameter_type_list:
parameter_declaration {
$$ = (parameter_list){ .first = $1 };
}
| parameter_type_list T_COMMA parameter_declaration {
$$ = $1;
$$.first = concat_symbols($1.first, $3);
}
;
abstract_declarator:
pointer {
$$ = add_pointer(abstract_name(), $1);
}
| direct_abstract_declarator
| pointer direct_abstract_declarator {
$$ = add_pointer($2, $1);
}
| type_attribute_list direct_abstract_declarator {
$$ = $2;
}
| pointer type_attribute_list direct_abstract_declarator {
$$ = add_pointer($3, $1);
}
;
direct_abstract_declarator:
T_LPAREN abstract_declarator T_RPAREN {
$$ = $2;
}
| T_LBRACK array_size_opt T_RBRACK {
$$ = add_array(abstract_name(), $2.has_dim, $2.dim);
}
| direct_abstract_declarator T_LBRACK array_size_opt T_RBRACK {
$$ = add_array($1, $3.has_dim, $3.dim);
}
| abstract_decl_param_list asm_or_symbolrename_opt {
sym_t *name = abstract_enclosing_name();
$$ = add_function(symbolrename(name, $2), $1);
end_declaration_level();
block_level--;
}
| direct_abstract_declarator abstract_decl_param_list
asm_or_symbolrename_opt {
$$ = add_function(symbolrename($1, $3), $2);
end_declaration_level();
block_level--;
}
| direct_abstract_declarator type_attribute_list
;
braced_initializer:
init_lbrace init_rbrace {
c23ism(353);
}
| init_lbrace initializer_list init_rbrace
| init_lbrace initializer_list T_COMMA init_rbrace
;
initializer:
assignment_expression {
init_expr($1);
}
| init_lbrace init_rbrace {
}
| init_lbrace initializer_list init_rbrace
| init_lbrace initializer_list T_COMMA init_rbrace
| error
;
initializer_list:
initializer
| designation initializer
| initializer_list T_COMMA initializer
| initializer_list T_COMMA designation initializer
;
designation:
{
begin_designation();
} designator_list T_ASSIGN
| identifier T_COLON {
gnuism(315);
begin_designation();
add_designator_member($1);
}
;
designator_list:
designator
| designator_list designator
;
designator:
T_LBRACK range T_RBRACK {
if (!allow_c99)
warning(321);
add_designator_subscript($2);
}
| T_POINT identifier {
if (!allow_c99)
warning(313);
add_designator_member($2);
}
;
static_assert_declaration:
T_STATIC_ASSERT T_LPAREN constant_expression T_COMMA T_STRING
T_RPAREN T_SEMI {
c11ism(354);
}
| T_STATIC_ASSERT T_LPAREN constant_expression T_RPAREN T_SEMI {
c23ism(355);
}
;
range:
constant_expression {
$$.lo = to_int_constant($1, true);
$$.hi = $$.lo;
}
| constant_expression T_ELLIPSIS constant_expression {
$$.lo = to_int_constant($1, true);
$$.hi = to_int_constant($3, true);
gnuism(340);
}
;
init_lbrace:
T_LBRACE {
init_lbrace();
}
;
init_rbrace:
T_RBRACE {
init_rbrace();
}
;
attribute_specifier_sequence:
attribute_specifier {
$$ = (attribute_list) { NULL, 0, 0 };
attribute_list_add_all(&$$, $1);
}
| attribute_specifier_sequence attribute_specifier {
$$ = $1;
attribute_list_add_all(&$$, $2);
}
;
attribute_specifier:
T_LBRACK T_LBRACK attribute_list T_RBRACK T_RBRACK {
$$ = $3;
}
;
attribute_list:
{
$$ = (attribute_list) { NULL, 0, 0 };
}
| attribute {
$$ = (attribute_list) { NULL, 0, 0 };
attribute_list_add(&$$, $1);
}
| attribute_list T_COMMA
| attribute_list T_COMMA attribute {
$$ = $1;
attribute_list_add(&$$, $3);
}
;
attribute:
identifier {
$$ = new_attribute(NULL, $1, NULL);
}
| identifier T_DCOLON identifier {
$$ = new_attribute($1, $3, NULL);
}
| identifier attribute_argument_clause {
$$ = new_attribute(NULL, $1, &$2);
}
| identifier T_DCOLON identifier attribute_argument_clause {
$$ = new_attribute($1, $3, &$4);
}
;
attribute_argument_clause:
T_LPAREN {
$$ = read_balanced_token_sequence();
}
;
asm_or_symbolrename_opt:
{
$$ = NULL;
}
| T_ASM T_LPAREN T_STRING T_RPAREN gcc_attribute_specifier_list_opt {
freeyyv(&$3, T_STRING);
$$ = NULL;
}
| T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN
gcc_attribute_specifier_list_opt {
$$ = $3;
}
;
statement:
expression_statement
| non_expr_statement
;
no_attr_statement:
expression_statement
| no_attr_non_expr_statement
;
non_expr_statement:
gcc_attribute_specifier T_SEMI
| no_attr_non_expr_statement
;
no_attr_non_expr_statement:
labeled_statement
| compound_statement
| selection_statement
| iteration_statement
| jump_statement {
suppress_fallthrough = false;
}
| asm_statement
;
label:
T_NAME T_COLON {
set_sym_kind(SK_LABEL);
named_label(getsym($1));
}
| T_CASE constant_expression T_COLON {
case_label($2);
suppress_fallthrough = true;
}
| T_CASE constant_expression T_ELLIPSIS constant_expression T_COLON {
case_label($2);
suppress_fallthrough = true;
}
| T_DEFAULT T_COLON {
default_label();
suppress_fallthrough = true;
}
;
labeled_statement:
label gcc_attribute_specifier_list_opt no_attr_statement
;
compound_statement:
compound_statement_lbrace compound_statement_rbrace
| compound_statement_lbrace block_item_list compound_statement_rbrace
;
compound_statement_lbrace:
T_LBRACE {
block_level++;
mem_block_level++;
debug_step("%s: mem_block_level = %zu",
"compound_statement_lbrace", mem_block_level);
begin_declaration_level(DLK_AUTO);
}
;
compound_statement_rbrace:
T_RBRACE {
end_declaration_level();
if (!in_statement_expr())
level_free_all(mem_block_level);
mem_block_level--;
debug_step("%s: mem_block_level = %zu",
"compound_statement_rbrace", mem_block_level);
block_level--;
suppress_fallthrough = false;
}
;
block_item_list:
block_item
| block_item_list block_item {
if ($1 && !$2)
c99ism(327);
$$ = $1 || $2;
}
;
block_item:
declaration_or_error {
$$ = false;
restore_warning_flags();
}
| statement {
$$ = true;
restore_warning_flags();
}
;
expression_statement:
expression T_SEMI {
expr($1, false, false, false, false,
$1 != NULL && $1->tn_op == CALL ? "call" : "expression");
suppress_fallthrough = false;
if ($1 != NULL && $1->tn_op == CALL
&& $1->u.call->func->tn_type->t_subt->t_noreturn)
stmt_call_noreturn();
}
| T_SEMI {
check_statement_reachable("empty");
suppress_fallthrough = false;
}
| attribute_specifier_sequence expression T_SEMI {
debug_attribute_list(&$1);
expr($2, false, false, false, false,
$2 != NULL && $2->tn_op == CALL ? "call" : "expression");
suppress_fallthrough = false;
}
| attribute_specifier_sequence T_SEMI {
bool is_fallthrough = attributes_contain(&$1, "fallthrough");
debug_attribute_list(&$1);
check_statement_reachable(
is_fallthrough ? "fallthrough" : "empty");
suppress_fallthrough = is_fallthrough;
}
;
selection_statement:
if_without_else %prec T_THEN {
save_warning_flags();
stmt_if_then_stmt();
stmt_if_else_stmt(false);
}
| if_without_else T_ELSE {
save_warning_flags();
stmt_if_then_stmt();
} statement {
restore_warning_flags();
stmt_if_else_stmt(true);
}
| if_without_else T_ELSE error {
clear_warning_flags();
stmt_if_else_stmt(false);
}
| switch_expr statement {
clear_warning_flags();
stmt_switch_expr_stmt();
}
| switch_expr error {
clear_warning_flags();
stmt_switch_expr_stmt();
}
;
if_without_else:
if_expr statement
| if_expr error
;
if_expr:
T_IF T_LPAREN expression T_RPAREN {
stmt_if_expr($3);
clear_warning_flags();
}
;
switch_expr:
T_SWITCH T_LPAREN expression T_RPAREN {
stmt_switch_expr($3);
clear_warning_flags();
}
;
iteration_statement:
while_expr statement {
clear_warning_flags();
stmt_while_expr_stmt();
}
| while_expr error {
clear_warning_flags();
stmt_while_expr_stmt();
}
| do_statement T_WHILE T_LPAREN expression T_RPAREN T_SEMI {
stmt_do_while_expr($4);
suppress_fallthrough = false;
}
| do error {
clear_warning_flags();
stmt_do_while_expr(NULL);
}
| for_exprs statement {
clear_warning_flags();
stmt_for_exprs_stmt();
end_declaration_level();
block_level--;
}
| for_exprs error {
clear_warning_flags();
stmt_for_exprs_stmt();
end_declaration_level();
block_level--;
}
;
while_expr:
T_WHILE T_LPAREN expression T_RPAREN {
stmt_while_expr($3);
clear_warning_flags();
}
;
do_statement:
do statement {
clear_warning_flags();
}
;
do:
T_DO {
stmt_do();
}
;
for_start:
T_FOR T_LPAREN {
begin_declaration_level(DLK_AUTO);
block_level++;
}
;
for_exprs:
for_start
begin_type_declaration_specifiers end_type
notype_init_declarator_list T_SEMI
expression_opt T_SEMI
expression_opt T_RPAREN {
c99ism(325);
stmt_for_exprs(NULL, $6, $8);
clear_warning_flags();
}
| for_start
expression_opt T_SEMI
expression_opt T_SEMI
expression_opt T_RPAREN {
stmt_for_exprs($2, $4, $6);
clear_warning_flags();
}
;
jump_statement:
goto identifier T_SEMI {
stmt_goto(getsym($2));
}
| goto error T_SEMI {
set_sym_kind(SK_VCFT);
}
| T_CONTINUE T_SEMI {
stmt_continue();
}
| T_BREAK T_SEMI {
stmt_break();
}
| T_RETURN sys T_SEMI {
stmt_return($2, NULL);
}
| T_RETURN sys expression T_SEMI {
stmt_return($2, $3);
}
;
goto:
T_GOTO {
set_sym_kind(SK_LABEL);
}
;
asm_statement:
T_ASM T_LPAREN read_until_rparen T_SEMI {
dcs_set_asm();
}
| T_ASM type_qualifier T_LPAREN read_until_rparen T_SEMI {
dcs_set_asm();
}
| T_ASM error
;
read_until_rparen:
{
read_until_rparen();
}
;
translation_unit:
external_declaration
| translation_unit external_declaration
;
external_declaration:
function_definition {
global_clean_up_decl(false);
clear_warning_flags();
}
| top_level_declaration {
global_clean_up_decl(false);
clear_warning_flags();
}
| asm_statement
| T_SEMI {
if (!allow_trad && !allow_c99)
error(0);
else if (allow_c90)
warning(0);
}
;
top_level_declaration:
begin_type end_type notype_init_declarator_list T_SEMI {
if (!allow_trad && !allow_c99)
error(1);
else if (allow_c90)
warning(1);
}
| declaration
| error T_SEMI {
global_clean_up();
}
| error T_RBRACE {
global_clean_up();
}
;
function_definition:
func_declarator {
if ($1->s_type->t_tspec != FUNC) {
error(249, yytext);
YYERROR;
}
if ($1->s_type->t_typedef) {
error(64);
YYERROR;
}
check_extern_declaration($1);
begin_function($1);
block_level++;
begin_declaration_level(DLK_OLD_STYLE_PARAMS);
if (lwarn == LWARN_NONE)
$1->s_used = true;
} arg_declaration_list_opt {
end_declaration_level();
block_level--;
check_func_lint_directives();
check_func_old_style_parameters();
begin_control_statement(CS_FUNCTION_BODY);
} compound_statement {
end_function();
end_control_statement(CS_FUNCTION_BODY);
}
;
func_declarator:
begin_type end_type notype_declarator {
if (!allow_trad)
error(1);
$$ = $3;
}
| begin_type_declmods end_type notype_declarator {
if (!allow_trad)
error(1);
$$ = $3;
}
| begin_type_declaration_specifiers end_type type_declarator {
$$ = $3;
}
;
arg_declaration_list_opt:
| arg_declaration_list
;
arg_declaration_list:
arg_declaration
| arg_declaration_list arg_declaration
| error
;
arg_declaration:
begin_type_declmods end_type T_SEMI {
warning(2);
}
| begin_type_declmods end_type notype_init_declarator_list T_SEMI
| begin_type_declaration_specifiers end_type T_SEMI {
if (!dcs->d_nonempty_decl)
warning(2);
else
warning(3, type_name(dcs->d_type));
}
| begin_type_declaration_specifiers end_type
type_init_declarator_list T_SEMI {
if (dcs->d_nonempty_decl)
warning(3, type_name(dcs->d_type));
}
| begin_type_declmods error
| begin_type_declaration_specifiers error
;
gcc_attribute_specifier_list_opt:
{
$$ = no_type_attributes();
}
| gcc_attribute_specifier_list
;
gcc_attribute_specifier_list:
gcc_attribute_specifier
| gcc_attribute_specifier_list gcc_attribute_specifier {
$$ = merge_type_attributes($1, $2);
}
;
gcc_attribute_specifier:
T_ATTRIBUTE T_LPAREN T_LPAREN {
in_gcc_attribute = true;
} gcc_attribute_list {
in_gcc_attribute = false;
} T_RPAREN T_RPAREN {
$$ = $5;
}
;
gcc_attribute_list:
gcc_attribute
| gcc_attribute_list T_COMMA gcc_attribute {
$$ = merge_type_attributes($1, $3);
}
;
gcc_attribute:
{
$$ = no_type_attributes();
}
| T_NAME {
$$ = no_type_attributes();
const char *name = $1->sb_name;
if (is_either(name, "packed", "__packed__"))
dcs_add_packed();
else if (is_either(name, "used", "__used__") ||
is_either(name, "constructor", "__constructor__") ||
is_either(name, "unused", "__unused__"))
$$.used = true;
else if (is_either(name, "fallthrough", "__fallthrough__"))
suppress_fallthrough = true;
else if (is_either(name, "noreturn", "__noreturn__"))
$$.noreturn = true;
}
| T_NAME T_LPAREN T_RPAREN {
$$ = (type_attributes){ .used = false };
}
| T_NAME T_LPAREN argument_expression_list T_RPAREN {
const char *name = $1->sb_name;
if (is_either(name, "aligned", "__aligned__")
&& $3->args_len == 1)
dcs_add_alignas(to_int_constant($3->args[0], true));
$$ = no_type_attributes();
if (is_either(name, "mode", "__mode__")
&& $3->args_len == 1
&& $3->args[0]->tn_op == NAME) {
const char *arg_name = $3->args[0]->u.sym->s_name;
if (strcmp(arg_name, "TI") == 0)
$$.bit_width = 128;
if (strcmp(arg_name, "DI") == 0)
$$.bit_width = 64;
}
}
| type_qualifier {
if (!$1.tq_const)
yyerror("Bad attribute");
$$ = no_type_attributes();
}
;
sys:
{
$$ = in_system_header;
}
;
%%
int
yyerror(const char *msg)
{
error(249, yytext);
if (++sytxerr >= 5)
norecover();
return 0;
}
#if YYDEBUG && YYBYACC
static const char *
cgram_to_string(int tok, YYSTYPE val)
{
switch (tok) {
case T_INCDEC:
return val.y_inc ? "++" : "--";
case T_MULTIPLICATIVE:
case T_ADDITIVE:
case T_SHIFT:
case T_RELATIONAL:
case T_EQUALITY:
case T_OPASSIGN:
return op_name(val.y_op);
case T_SCLASS:
return scl_name(val.y_scl);
case T_TYPE:
case T_STRUCT_OR_UNION:
return tspec_name(val.y_tspec);
case T_QUAL:
return type_qualifiers_string(val.y_type_qualifiers);
case T_FUNCTION_SPECIFIER:
return function_specifier_name(val.y_function_specifier);
case T_NAME:
return val.y_name->sb_name;
default:
return "<none>";
}
}
#endif
static void
cgram_declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
{
declare(decl, has_initializer, renaming);
if (renaming != NULL)
freeyyv(&renaming, T_NAME);
}
static void
read_until_rparen(void)
{
int level;
if (yychar < 0)
yychar = yylex();
freeyyv(&yylval, yychar);
level = 1;
while (yychar > 0) {
if (yychar == T_LPAREN)
level++;
if (yychar == T_RPAREN && --level == 0)
break;
freeyyv(&yylval, yychar = yylex());
}
yyclearin;
}
static balanced_token_sequence
read_balanced_token_sequence(void)
{
lint_assert(yychar < 0);
balanced_token_sequence seq = lex_balanced();
yyclearin;
return seq;
}
static sym_t *
symbolrename(sym_t *s, sbuf_t *sb)
{
if (sb != NULL)
s->s_rename = sb->sb_name;
return s;
}