+#if 0
+static designator_t *parse_designation(void)
+{
+ if(token.type != '[' && token.type != '.')
+ return NULL;
+
+ designator_t *result = NULL;
+ designator_t *last = NULL;
+
+ while(1) {
+ designator_t *designator;
+ switch(token.type) {
+ case '[':
+ designator = allocate_ast_zero(sizeof(designator[0]));
+ next_token();
+ designator->array_access = parse_constant_expression();
+ expect(']');
+ break;
+ case '.':
+ designator = allocate_ast_zero(sizeof(designator[0]));
+ next_token();
+ if(token.type != T_IDENTIFIER) {
+ parse_error_expected("while parsing designator",
+ T_IDENTIFIER, 0);
+ return NULL;
+ }
+ designator->symbol = token.v.symbol;
+ next_token();
+ break;
+ default:
+ expect('=');
+ return result;
+ }
+
+ assert(designator != NULL);
+ if(last != NULL) {
+ last->next = designator;
+ } else {
+ result = designator;
+ }
+ last = designator;
+ }
+}
+#endif
+
+static initializer_t *initializer_from_string(array_type_t *type,
+ const char *string)
+{
+ /* TODO: check len vs. size of array type */
+ (void) type;
+
+ initializer_string_t *initializer
+ = allocate_ast_zero(sizeof(initializer[0]));
+
+ initializer->initializer.type = INITIALIZER_STRING;
+ initializer->string = string;
+
+ return (initializer_t*) initializer;
+}
+
+static initializer_t *initializer_from_expression(type_t *type,
+ expression_t *expression)
+{
+
+ /* TODO check that expression is a constant expression */
+
+ /* § 6.7.8.14/15 char array may be initialized by string literals */
+ if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
+ array_type_t *array_type = (array_type_t*) type;
+ type_t *element_type = array_type->element_type;
+
+ if(element_type->type == TYPE_ATOMIC) {
+ atomic_type_t *atomic_type = (atomic_type_t*) element_type;
+ atomic_type_type_t atype = atomic_type->atype;
+
+ /* TODO handle wide strings */
+ if(atype == ATOMIC_TYPE_CHAR
+ || atype == ATOMIC_TYPE_SCHAR
+ || atype == ATOMIC_TYPE_UCHAR) {
+
+ string_literal_t *literal = (string_literal_t*) expression;
+ return initializer_from_string(array_type, literal->value);
+ }
+ }
+ }
+
+ semantic_assign(type, &expression, "initializer");
+
+ initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
+ result->initializer.type = INITIALIZER_VALUE;
+ result->value = expression;
+
+ return (initializer_t*) result;
+}
+
+static initializer_t *parse_sub_initializer(type_t *type,
+ expression_t *expression,
+ type_t *expression_type);
+
+static initializer_t *parse_sub_initializer_elem(type_t *type)
+{
+ if(token.type == '{') {
+ return parse_sub_initializer(type, NULL, NULL);
+ }
+
+ expression_t *expression = parse_assignment_expression();
+ type_t *expression_type = skip_typeref(expression->datatype);
+
+ return parse_sub_initializer(type, expression, expression_type);
+}
+
+static bool had_initializer_brace_warning;
+
+static initializer_t *parse_sub_initializer(type_t *type,
+ expression_t *expression,
+ type_t *expression_type)
+{
+ if(is_type_scalar(type)) {
+ /* there might be extra {} hierarchies */
+ if(token.type == '{') {
+ next_token();
+ if(!had_initializer_brace_warning) {
+ parse_warning("braces around scalar initializer");
+ had_initializer_brace_warning = true;
+ }
+ initializer_t *result = parse_sub_initializer(type, NULL, NULL);
+ if(token.type == ',') {
+ next_token();
+ /* TODO: warn about excessive elements */
+ }
+ expect_block('}');
+ return result;
+ }
+
+ if(expression == NULL) {
+ expression = parse_assignment_expression();
+ }
+ return initializer_from_expression(type, expression);
+ }
+
+ /* TODO: ignore qualifiers, comparing pointers is probably
+ * not correct */
+ if(expression != NULL && expression_type == type) {
+ initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
+ result->initializer.type = INITIALIZER_VALUE;
+
+ if(type != NULL) {
+ semantic_assign(type, &expression, "initializer");
+ }
+ result->value = expression;
+
+ return (initializer_t*) result;
+ }
+
+ bool read_paren = false;
+ if(token.type == '{') {
+ next_token();
+ read_paren = true;
+ }
+
+ /* descend into subtype */
+ initializer_t *result = NULL;
+ initializer_t **elems;
+ if(type->type == TYPE_ARRAY) {
+ array_type_t *array_type = (array_type_t*) type;
+ type_t *element_type = array_type->element_type;
+ element_type = skip_typeref(element_type);
+
+ initializer_t *sub;
+ had_initializer_brace_warning = false;
+ if(expression == NULL) {
+ sub = parse_sub_initializer_elem(element_type);
+ } else {
+ sub = parse_sub_initializer(element_type, expression,
+ expression_type);
+ }
+
+ /* didn't match the subtypes -> try the parent type */
+ if(sub == NULL) {
+ assert(!read_paren);
+ return NULL;
+ }
+
+ elems = NEW_ARR_F(initializer_t*, 0);
+ ARR_APP1(initializer_t*, elems, sub);
+
+ while(true) {
+ if(token.type == '}')
+ break;
+ expect_block(',');
+ if(token.type == '}')
+ break;
+
+ initializer_t *sub
+ = parse_sub_initializer(element_type, NULL, NULL);
+ if(sub == NULL) {
+ /* TODO error, do nicer cleanup */
+ parse_error("member initializer didn't match");
+ DEL_ARR_F(elems);
+ return NULL;
+ }
+ ARR_APP1(initializer_t*, elems, sub);
+ }
+ } else {
+ assert(type->type == TYPE_COMPOUND_STRUCT
+ || type->type == TYPE_COMPOUND_UNION);
+ compound_type_t *compound_type = (compound_type_t*) type;
+ context_t *context = & compound_type->declaration->context;
+
+ declaration_t *first = context->declarations;
+ if(first == NULL)
+ return NULL;
+ type_t *first_type = first->type;
+ first_type = skip_typeref(first_type);
+
+ initializer_t *sub;
+ had_initializer_brace_warning = false;
+ if(expression == NULL) {
+ sub = parse_sub_initializer_elem(first_type);
+ } else {
+ sub = parse_sub_initializer(first_type, expression,expression_type);
+ }
+
+ /* didn't match the subtypes -> try our parent type */
+ if(sub == NULL) {
+ assert(!read_paren);
+ return NULL;
+ }
+
+ elems = NEW_ARR_F(initializer_t*, 0);
+ ARR_APP1(initializer_t*, elems, sub);
+
+ declaration_t *iter = first->next;
+ for( ; iter != NULL; iter = iter->next) {
+ if(iter->symbol == NULL)
+ continue;
+ if(iter->namespc != NAMESPACE_NORMAL)
+ continue;
+
+ if(token.type == '}')
+ break;
+ expect_block(',');
+
+ type_t *iter_type = iter->type;
+ iter_type = skip_typeref(iter_type);
+
+ initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
+ if(sub == NULL) {
+ /* TODO error, do nicer cleanup*/
+ parse_error("member initializer didn't match");
+ DEL_ARR_F(elems);
+ return NULL;
+ }
+ ARR_APP1(initializer_t*, elems, sub);
+ }
+ }
+
+ int len = ARR_LEN(elems);
+ size_t elems_size = sizeof(initializer_t*) * len;
+
+ initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
+
+ 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;
+}
+