+
+ /* declarator is common to both function-definitions and declarations */
+ declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
+
+ /* must be a declaration */
+ if(token.type == ',' || token.type == '=' || token.type == ';') {
+ parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
+ return;
+ }
+
+ /* must be a function definition */
+ parse_kr_declaration_list(ndeclaration);
+
+ if(token.type != '{') {
+ parse_error_expected("while parsing function definition", '{', 0);
+ eat_statement();
+ return;
+ }
+
+ type_t *type = ndeclaration->type;
+
+ /* note that we don't skip typerefs: the standard doesn't allow them here
+ * (so we can't use is_type_function here) */
+ if(type->kind != TYPE_FUNCTION) {
+ if (is_type_valid(type)) {
+ errorf(HERE, "declarator '%#T' has a body but is not a function type",
+ type, ndeclaration->symbol);
+ }
+ eat_block();
+ return;
+ }
+
+ /* § 6.7.5.3 (14) a function definition with () means no
+ * parameters (and not unspecified parameters) */
+ if(type->function.unspecified_parameters) {
+ type_t *duplicate = duplicate_type(type);
+ duplicate->function.unspecified_parameters = false;
+
+ type = typehash_insert(duplicate);
+ if(type != duplicate) {
+ obstack_free(type_obst, duplicate);
+ }
+ ndeclaration->type = type;
+ }
+
+ declaration_t *const declaration = record_function_definition(ndeclaration);
+ if(ndeclaration != declaration) {
+ declaration->context = ndeclaration->context;
+ }
+ type = skip_typeref(declaration->type);
+
+ /* push function parameters and switch context */
+ int top = environment_top();
+ context_t *last_context = context;
+ set_context(&declaration->context);
+
+ declaration_t *parameter = declaration->context.declarations;
+ for( ; parameter != NULL; parameter = parameter->next) {
+ if(parameter->parent_context == &ndeclaration->context) {
+ parameter->parent_context = context;
+ }
+ assert(parameter->parent_context == NULL
+ || parameter->parent_context == context);
+ parameter->parent_context = context;
+ environment_push(parameter);
+ }
+
+ if(declaration->init.statement != NULL) {
+ parser_error_multiple_definition(declaration, token.source_position);
+ eat_block();
+ goto end_of_parse_external_declaration;
+ } else {
+ /* parse function body */
+ int label_stack_top = label_top();
+ declaration_t *old_current_function = current_function;
+ current_function = declaration;
+
+ declaration->init.statement = parse_compound_statement();
+ check_for_missing_labels();
+
+ assert(current_function == declaration);
+ current_function = old_current_function;
+ label_pop_to(label_stack_top);
+ }
+
+end_of_parse_external_declaration:
+ assert(context == &declaration->context);
+ set_context(last_context);
+ environment_pop_to(top);
+}
+
+static type_t *make_bitfield_type(type_t *base, expression_t *size)
+{
+ type_t *type = allocate_type_zero(TYPE_BITFIELD);
+ type->bitfield.base = base;
+ type->bitfield.size = size;
+
+ return type;
+}
+
+static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
+{
+ /* TODO: check constraints for struct declarations (in specifiers) */
+ while(1) {
+ declaration_t *declaration;
+
+ if(token.type == ':') {
+ next_token();
+
+ type_t *base_type = specifiers->type;
+ expression_t *size = parse_constant_expression();
+
+ type_t *type = make_bitfield_type(base_type, size);
+
+ declaration = allocate_declaration_zero();
+ declaration->namespc = NAMESPACE_NORMAL;
+ declaration->storage_class = STORAGE_CLASS_NONE;
+ declaration->source_position = token.source_position;
+ declaration->modifiers = specifiers->decl_modifiers;
+ declaration->type = type;
+ } else {
+ declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
+
+ if(token.type == ':') {
+ next_token();
+ expression_t *size = parse_constant_expression();
+
+ type_t *type = make_bitfield_type(declaration->type, size);
+ declaration->type = type;
+ }
+ }
+ record_declaration(declaration);
+
+ if(token.type != ',')
+ break;
+ next_token();
+ }
+ expect_void(';');
+}
+
+static void parse_compound_type_entries(void)
+{
+ eat('{');
+
+ while(token.type != '}' && token.type != T_EOF) {
+ declaration_specifiers_t specifiers;
+ memset(&specifiers, 0, sizeof(specifiers));
+ parse_declaration_specifiers(&specifiers);
+
+ parse_struct_declarators(&specifiers);
+ }
+ if(token.type == T_EOF) {
+ errorf(HERE, "EOF while parsing struct");
+ }
+ next_token();