+ init->initializer.type = INITIALIZER_LIST;
+ init->len = len;
+ memcpy(init->initializers, elems, elems_size);
+ DEL_ARR_F(elems);
+
+ result = (initializer_t*) init;
+
+ if(read_paren) {
+ if(token.type == ',')
+ next_token();
+ expect('}');
+ }
+ return result;
+}
+
+static initializer_t *parse_initializer(type_t *type)
+{
+ initializer_t *result;
+
+ type = skip_typeref(type);
+
+ if(token.type != '{') {
+ expression_t *expression = parse_assignment_expression();
+ return initializer_from_expression(type, expression);
+ }
+
+ if(is_type_scalar(type)) {
+ /* § 6.7.8.11 */
+ eat('{');
+
+ expression_t *expression = parse_assignment_expression();
+ result = initializer_from_expression(type, expression);
+
+ if(token.type == ',')
+ next_token();
+
+ expect('}');
+ return result;
+ } else {
+ result = parse_sub_initializer(type, NULL, NULL);
+ }
+
+ return result;
+}
+
+
+
+static declaration_t *parse_compound_type_specifier(bool is_struct)
+{
+ if(is_struct) {
+ eat(T_struct);
+ } else {
+ eat(T_union);
+ }
+
+ symbol_t *symbol = NULL;
+ declaration_t *declaration = NULL;
+
+ if (token.type == T___attribute__) {
+ /* TODO */
+ parse_attributes();
+ }
+
+ if(token.type == T_IDENTIFIER) {
+ symbol = token.v.symbol;
+ next_token();
+
+ if(is_struct) {
+ declaration = get_declaration(symbol, NAMESPACE_STRUCT);
+ } else {
+ declaration = get_declaration(symbol, NAMESPACE_UNION);
+ }
+ } else if(token.type != '{') {
+ if(is_struct) {
+ parse_error_expected("while parsing struct type specifier",
+ T_IDENTIFIER, '{', 0);
+ } else {
+ parse_error_expected("while parsing union type specifier",
+ T_IDENTIFIER, '{', 0);
+ }
+
+ return NULL;
+ }
+
+ if(declaration == NULL) {
+ declaration = allocate_type_zero(sizeof(declaration[0]));
+
+ if(is_struct) {
+ declaration->namespc = NAMESPACE_STRUCT;
+ } else {
+ declaration->namespc = NAMESPACE_UNION;
+ }
+ declaration->source_position = token.source_position;
+ declaration->symbol = symbol;
+ record_declaration(declaration);
+ }
+
+ if(token.type == '{') {
+ if(declaration->init.is_defined) {
+ assert(symbol != NULL);
+ parser_print_error_prefix();
+ fprintf(stderr, "multiple definition of %s %s\n",
+ is_struct ? "struct" : "union", symbol->string);
+ declaration->context.declarations = NULL;
+ }
+ declaration->init.is_defined = true;
+
+ int top = environment_top();
+ context_t *last_context = context;
+ set_context(& declaration->context);
+
+ parse_compound_type_entries();
+ parse_attributes();
+
+ assert(context == & declaration->context);
+ set_context(last_context);
+ environment_pop_to(top);
+ }
+
+ return declaration;
+}
+
+static void parse_enum_entries(type_t *enum_type)
+{
+ eat('{');
+
+ if(token.type == '}') {
+ next_token();
+ parse_error("empty enum not allowed");
+ return;
+ }
+
+ do {
+ declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
+
+ if(token.type != T_IDENTIFIER) {
+ parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
+ eat_block();
+ return;
+ }
+ entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
+ entry->type = enum_type;
+ entry->symbol = token.v.symbol;
+ entry->source_position = token.source_position;
+ next_token();
+
+ if(token.type == '=') {
+ next_token();
+ entry->init.enum_value = parse_constant_expression();
+
+ /* TODO semantic */
+ }
+
+ record_declaration(entry);
+
+ if(token.type != ',')
+ break;
+ next_token();
+ } while(token.type != '}');
+
+ expect_void('}');
+}
+
+static declaration_t *parse_enum_specifier(void)
+{
+ eat(T_enum);
+
+ declaration_t *declaration;
+ symbol_t *symbol;
+
+ if(token.type == T_IDENTIFIER) {
+ symbol = token.v.symbol;
+ next_token();
+
+ declaration = get_declaration(symbol, NAMESPACE_ENUM);
+ } else if(token.type != '{') {
+ parse_error_expected("while parsing enum type specifier",
+ T_IDENTIFIER, '{', 0);
+ return NULL;
+ } else {
+ declaration = NULL;
+ symbol = NULL;
+ }
+
+ if(declaration == NULL) {
+ declaration = allocate_type_zero(sizeof(declaration[0]));
+
+ declaration->namespc = NAMESPACE_ENUM;
+ declaration->source_position = token.source_position;
+ declaration->symbol = symbol;
+ }
+
+ if(token.type == '{') {
+ if(declaration->init.is_defined) {
+ parser_print_error_prefix();
+ fprintf(stderr, "multiple definitions of enum %s\n",
+ symbol->string);
+ }
+ record_declaration(declaration);
+ declaration->init.is_defined = 1;
+
+ parse_enum_entries(NULL);
+ parse_attributes();
+ }
+
+ return declaration;
+}
+
+/**
+ * if a symbol is a typedef to another type, return true
+ */
+static bool is_typedef_symbol(symbol_t *symbol)
+{
+ const declaration_t *const declaration =
+ get_declaration(symbol, NAMESPACE_NORMAL);
+ return
+ declaration != NULL &&
+ declaration->storage_class == STORAGE_CLASS_TYPEDEF;
+}
+
+static type_t *parse_typeof(void)
+{
+ eat(T___typeof__);
+
+ type_t *type;
+
+ expect('(');
+
+ expression_t *expression = NULL;
+
+restart:
+ switch(token.type) {
+ case T___extension__:
+ /* this can be a prefix to a typename or an expression */
+ /* we simply eat it now. */
+ do {
+ next_token();
+ } while(token.type == T___extension__);
+ goto restart;
+
+ case T_IDENTIFIER:
+ if(is_typedef_symbol(token.v.symbol)) {
+ type = parse_typename();
+ } else {
+ expression = parse_expression();
+ type = expression->datatype;
+ }
+ break;
+
+ TYPENAME_START
+ type = parse_typename();
+ break;
+
+ default:
+ expression = parse_expression();
+ type = expression->datatype;
+ break;
+ }
+
+ expect(')');
+
+ typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
+ typeof->type.type = TYPE_TYPEOF;
+ typeof->expression = expression;
+ typeof->typeof_type = type;
+
+ return (type_t*) typeof;
+}
+
+typedef enum {
+ SPECIFIER_SIGNED = 1 << 0,
+ SPECIFIER_UNSIGNED = 1 << 1,
+ SPECIFIER_LONG = 1 << 2,
+ SPECIFIER_INT = 1 << 3,
+ SPECIFIER_DOUBLE = 1 << 4,
+ SPECIFIER_CHAR = 1 << 5,
+ SPECIFIER_SHORT = 1 << 6,
+ SPECIFIER_LONG_LONG = 1 << 7,
+ SPECIFIER_FLOAT = 1 << 8,
+ SPECIFIER_BOOL = 1 << 9,
+ SPECIFIER_VOID = 1 << 10,
+#ifdef PROVIDE_COMPLEX
+ SPECIFIER_COMPLEX = 1 << 11,
+ SPECIFIER_IMAGINARY = 1 << 12,
+#endif
+} specifiers_t;
+
+static type_t *create_builtin_type(symbol_t *symbol)
+{
+ builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
+ type->type.type = TYPE_BUILTIN;
+ type->symbol = symbol;
+ /* TODO... */
+ type->real_type = type_int;
+
+ return (type_t*) type;
+}
+
+static type_t *get_typedef_type(symbol_t *symbol)
+{
+ declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
+ if(declaration == NULL
+ || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
+ return NULL;
+
+ typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
+ typedef_type->type.type = TYPE_TYPEDEF;
+ typedef_type->declaration = declaration;
+
+ return (type_t*) typedef_type;
+}
+
+static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
+{
+ type_t *type = NULL;
+ unsigned type_qualifiers = 0;
+ unsigned type_specifiers = 0;
+ int newtype = 0;
+
+ while(true) {
+ switch(token.type) {
+
+ /* storage class */
+#define MATCH_STORAGE_CLASS(token, class) \
+ case token: \
+ if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
+ parse_error("multiple storage classes in declaration " \
+ "specifiers"); \
+ } \
+ specifiers->storage_class = class; \
+ next_token(); \
+ break;
+
+ MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
+ MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
+ MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
+ MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
+ MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
+
+ /* type qualifiers */
+#define MATCH_TYPE_QUALIFIER(token, qualifier) \
+ case token: \
+ type_qualifiers |= qualifier; \
+ next_token(); \
+ break;
+
+ MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
+ MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
+ MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
+
+ case T___extension__:
+ /* TODO */
+ next_token();
+ break;
+
+ /* type specifiers */
+#define MATCH_SPECIFIER(token, specifier, name) \
+ case token: \
+ next_token(); \
+ if(type_specifiers & specifier) { \
+ parse_error("multiple " name " type specifiers given"); \
+ } else { \
+ type_specifiers |= specifier; \
+ } \
+ break;
+
+ MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
+ MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
+ MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
+ MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
+ MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
+ MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
+ MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
+ MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
+ MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
+#ifdef PROVIDE_COMPLEX
+ MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
+ MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
+#endif
+ case T_inline:
+ next_token();
+ specifiers->is_inline = true;
+ break;
+
+ case T_long:
+ next_token();
+ if(type_specifiers & SPECIFIER_LONG_LONG) {
+ parse_error("multiple type specifiers given");
+ } else if(type_specifiers & SPECIFIER_LONG) {
+ type_specifiers |= SPECIFIER_LONG_LONG;
+ } else {
+ type_specifiers |= SPECIFIER_LONG;
+ }
+ break;
+
+ /* TODO: if type != NULL for the following rules should issue
+ * an error */
+ case T_struct: {
+ compound_type_t *compound_type
+ = allocate_type_zero(sizeof(compound_type[0]));
+ compound_type->type.type = TYPE_COMPOUND_STRUCT;
+ compound_type->declaration = parse_compound_type_specifier(true);
+
+ type = (type_t*) compound_type;
+ break;
+ }
+ case T_union: {
+ compound_type_t *compound_type
+ = allocate_type_zero(sizeof(compound_type[0]));
+ compound_type->type.type = TYPE_COMPOUND_UNION;
+ compound_type->declaration = parse_compound_type_specifier(false);
+
+ type = (type_t*) compound_type;
+ break;
+ }
+ case T_enum: {
+ enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
+ enum_type->type.type = TYPE_ENUM;
+ enum_type->declaration = parse_enum_specifier();
+
+ type = (type_t*) enum_type;
+ break;
+ }
+ case T___typeof__:
+ type = parse_typeof();
+ break;
+ case T___builtin_va_list:
+ type = create_builtin_type(token.v.symbol);
+ next_token();
+ break;
+
+ case T___attribute__:
+ /* TODO */
+ parse_attributes();
+ break;
+
+ case T_IDENTIFIER: {
+ type_t *typedef_type = get_typedef_type(token.v.symbol);
+
+ if(typedef_type == NULL)
+ goto finish_specifiers;
+
+ next_token();
+ type = typedef_type;
+ break;
+ }
+
+ /* function specifier */
+ default:
+ goto finish_specifiers;
+ }
+ }
+
+finish_specifiers:
+
+ if(type == NULL) {
+ atomic_type_type_t atomic_type;
+
+ /* match valid basic types */
+ switch(type_specifiers) {
+ case SPECIFIER_VOID:
+ atomic_type = ATOMIC_TYPE_VOID;
+ break;
+ case SPECIFIER_CHAR:
+ atomic_type = ATOMIC_TYPE_CHAR;
+ break;
+ case SPECIFIER_SIGNED | SPECIFIER_CHAR:
+ atomic_type = ATOMIC_TYPE_SCHAR;
+ break;
+ case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
+ atomic_type = ATOMIC_TYPE_UCHAR;
+ break;
+ case SPECIFIER_SHORT:
+ case SPECIFIER_SIGNED | SPECIFIER_SHORT:
+ case SPECIFIER_SHORT | SPECIFIER_INT:
+ case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
+ atomic_type = ATOMIC_TYPE_SHORT;