correctly fix calling convention problems from bug #42
[cparser] / parser.c
index 078291d..1f3173f 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -2532,11 +2532,12 @@ static compound_t *parse_compound_type_specifier(bool is_struct)
                eat(T_union);
        }
 
-       symbol_t   *symbol   = NULL;
-       compound_t *compound = NULL;
+       symbol_t    *symbol   = NULL;
+       compound_t  *compound = NULL;
+       attribute_t *attributes = NULL;
 
        if (token.type == T___attribute__) {
-               parse_attributes(NULL);
+               attributes = parse_attributes(NULL);
        }
 
        entity_kind_tag_t const kind = is_struct ? ENTITY_STRUCT : ENTITY_UNION;
@@ -2590,7 +2591,6 @@ static compound_t *parse_compound_type_specifier(bool is_struct)
 
        if (token.type == '{') {
                parse_compound_type_entries(compound);
-               parse_attributes(NULL);
 
                /* ISO/IEC 14882:1998(E) ยง7.1.3:5 */
                if (symbol == NULL) {
@@ -2599,6 +2599,10 @@ static compound_t *parse_compound_type_specifier(bool is_struct)
                }
        }
 
+       if (attributes != NULL) {
+               handle_entity_attributes(attributes, (entity_t*) compound);
+       }
+
        return compound;
 }
 
@@ -2978,101 +2982,6 @@ static entity_t *create_error_entity(symbol_t *symbol, entity_kind_tag_t kind)
        return entity;
 }
 
-/**
- * Finish the construction of a struct type by calculating its size, offsets,
- * alignment.
- */
-static void finish_struct_type(compound_type_t *type)
-{
-       assert(type->compound != NULL);
-
-       compound_t *compound = type->compound;
-       if (!compound->complete)
-               return;
-
-       il_size_t      size = 0;
-       il_size_t      offset;
-       il_alignment_t alignment = compound->alignment;
-       bool           need_pad  = false;
-
-       entity_t *entry = compound->members.entities;
-       for (; entry != NULL; entry = entry->base.next) {
-               if (entry->kind != ENTITY_COMPOUND_MEMBER)
-                       continue;
-
-               type_t *m_type = entry->declaration.type;
-               if (! is_type_valid(skip_typeref(m_type))) {
-                       /* simply ignore errors here */
-                       continue;
-               }
-               il_alignment_t m_alignment = get_type_alignment(m_type);
-               if (m_alignment > alignment)
-                       alignment = m_alignment;
-
-               offset = (size + m_alignment - 1) & -m_alignment;
-
-               if (offset > size)
-                       need_pad = true;
-               entry->compound_member.offset = offset;
-               size = offset + get_type_size(m_type);
-       }
-
-       offset = (size + alignment - 1) & -alignment;
-       if (offset > size)
-               need_pad = true;
-
-       if (need_pad) {
-               if (warning.padded) {
-                       warningf(&compound->base.source_position, "'%T' needs padding",
-                                type);
-               }
-       } else if (compound->packed && warning.packed) {
-               warningf(&compound->base.source_position,
-                        "superfluous packed attribute on '%T'", type);
-       }
-
-       compound->size      = offset;
-       compound->alignment = alignment;
-}
-
-/**
- * Finish the construction of an union type by calculating
- * its size and alignment.
- */
-static void finish_union_type(compound_type_t *type)
-{
-       assert(type->compound != NULL);
-
-       compound_t *compound = type->compound;
-       if (! compound->complete)
-               return;
-
-       il_size_t      size      = 0;
-       il_alignment_t alignment = compound->alignment;
-
-       entity_t *entry = compound->members.entities;
-       for (; entry != NULL; entry = entry->base.next) {
-               if (entry->kind != ENTITY_COMPOUND_MEMBER)
-                       continue;
-
-               type_t *m_type = entry->declaration.type;
-               if (! is_type_valid(skip_typeref(m_type)))
-                       continue;
-
-               entry->compound_member.offset = 0;
-               il_size_t m_size = get_type_size(m_type);
-               if (m_size > size)
-                       size = m_size;
-               il_alignment_t m_alignment = get_type_alignment(m_type);
-               if (m_alignment > alignment)
-                       alignment = m_alignment;
-       }
-       size = (size + alignment - 1) & -alignment;
-
-       compound->size      = size;
-       compound->alignment = alignment;
-}
-
 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
 {
        type_t            *type              = NULL;
@@ -3218,13 +3127,11 @@ wrong_thread_stoarge_class:
                        type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
 
                        type->compound.compound = parse_compound_type_specifier(true);
-                       finish_struct_type(&type->compound);
                        break;
                case T_union:
                        CHECK_DOUBLE_TYPE();
                        type = allocate_type_zero(TYPE_COMPOUND_UNION);
                        type->compound.compound = parse_compound_type_specifier(false);
-                       finish_union_type(&type->compound);
                        break;
                case T_enum:
                        CHECK_DOUBLE_TYPE();
@@ -3816,21 +3723,8 @@ static construct_type_t *parse_function_declarator(scope_t *scope)
        type_t          *type  = allocate_type_zero(TYPE_FUNCTION);
        function_type_t *ftype = &type->function;
 
-       ftype->linkage = current_linkage;
-
-#if 0
-       switch (modifiers & (DM_CDECL | DM_STDCALL | DM_FASTCALL | DM_THISCALL)) {
-               case DM_NONE:     break;
-               case DM_CDECL:    ftype->calling_convention = CC_CDECL;    break;
-               case DM_STDCALL:  ftype->calling_convention = CC_STDCALL;  break;
-               case DM_FASTCALL: ftype->calling_convention = CC_FASTCALL; break;
-               case DM_THISCALL: ftype->calling_convention = CC_THISCALL; break;
-
-               default:
-                       errorf(HERE, "multiple calling conventions in declaration");
-                       break;
-       }
-#endif
+       ftype->linkage            = current_linkage;
+       ftype->calling_convention = CC_CDECL;
 
        parse_parameters(ftype, scope);
 
@@ -5884,13 +5778,17 @@ static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
 
        if (is_constant_expression(size)) {
                long v = fold_constant_to_int(size);
+               const symbol_t *user_symbol = symbol == NULL ? sym_anonymous : symbol;
 
                if (v < 0) {
-                       errorf(source_position, "negative width in bit-field '%Y'", symbol);
-               } else if (v == 0) {
-                       errorf(source_position, "zero width for bit-field '%Y'", symbol);
+                       errorf(source_position, "negative width in bit-field '%Y'",
+                              user_symbol);
+               } else if (v == 0 && symbol != NULL) {
+                       errorf(source_position, "zero width for bit-field '%Y'",
+                              user_symbol);
                } else if (bit_size > 0 && (il_size_t)v > bit_size) {
-                       errorf(source_position, "width of '%Y' exceeds its type", symbol);
+                       errorf(source_position, "width of '%Y' exceeds its type",
+                              user_symbol);
                } else {
                        type->bitfield.bit_size = v;
                }
@@ -5909,12 +5807,12 @@ static entity_t *find_compound_entry(compound_t *compound, symbol_t *symbol)
                if (iter->base.symbol == symbol) {
                        return iter;
                } else if (iter->base.symbol == NULL) {
+                       /* search in anonymous structs and unions */
                        type_t *type = skip_typeref(iter->declaration.type);
                        if (is_type_compound(type)) {
-                               entity_t *result
-                                       = find_compound_entry(type->compound.compound, symbol);
-                               if (result != NULL)
-                                       return result;
+                               if (find_compound_entry(type->compound.compound, symbol)
+                                               != NULL)
+                                       return iter;
                        }
                        continue;
                }
@@ -5923,6 +5821,98 @@ static entity_t *find_compound_entry(compound_t *compound, symbol_t *symbol)
        return NULL;
 }
 
+static void check_deprecated(const source_position_t *source_position,
+                             const entity_t *entity)
+{
+       if (!warning.deprecated_declarations)
+               return;
+       if (!is_declaration(entity))
+               return;
+       if ((entity->declaration.modifiers & DM_DEPRECATED) == 0)
+               return;
+
+       char const *const prefix = get_entity_kind_name(entity->kind);
+       const char *deprecated_string
+                       = get_deprecated_string(entity->declaration.attributes);
+       if (deprecated_string != NULL) {
+               warningf(source_position, "%s '%Y' is deprecated (declared %P): \"%s\"",
+                                prefix, entity->base.symbol, &entity->base.source_position,
+                                deprecated_string);
+       } else {
+               warningf(source_position, "%s '%Y' is deprecated (declared %P)", prefix,
+                                entity->base.symbol, &entity->base.source_position);
+       }
+}
+
+
+static expression_t *create_select(const source_position_t *pos,
+                                   expression_t *addr,
+                                   type_qualifiers_t qualifiers,
+                                                                  entity_t *entry)
+{
+       assert(entry->kind == ENTITY_COMPOUND_MEMBER);
+
+       check_deprecated(pos, entry);
+
+       expression_t *select          = allocate_expression_zero(EXPR_SELECT);
+       select->select.compound       = addr;
+       select->select.compound_entry = entry;
+
+       type_t *entry_type = entry->declaration.type;
+       type_t *res_type   = get_qualified_type(entry_type, qualifiers);
+
+       /* we always do the auto-type conversions; the & and sizeof parser contains
+        * code to revert this! */
+       select->base.type = automatic_type_conversion(res_type);
+       if (res_type->kind == TYPE_BITFIELD) {
+               select->base.type = res_type->bitfield.base_type;
+       }
+
+       return select;
+}
+
+/**
+ * Find entry with symbol in compound. Search anonymous structs and unions and
+ * creates implicit select expressions for them.
+ * Returns the adress for the innermost compound.
+ */
+static expression_t *find_create_select(const source_position_t *pos,
+                                        expression_t *addr,
+                                        type_qualifiers_t qualifiers,
+                                        compound_t *compound, symbol_t *symbol)
+{
+       entity_t *iter = compound->members.entities;
+       for (; iter != NULL; iter = iter->base.next) {
+               if (iter->kind != ENTITY_COMPOUND_MEMBER)
+                       continue;
+
+               symbol_t *iter_symbol = iter->base.symbol;
+               if (iter_symbol == NULL) {
+                       type_t *type = iter->declaration.type;
+                       if (type->kind != TYPE_COMPOUND_STRUCT
+                                       && type->kind != TYPE_COMPOUND_UNION)
+                               continue;
+
+                       compound_t *sub_compound = type->compound.compound;
+
+                       if (find_compound_entry(sub_compound, symbol) == NULL)
+                               continue;
+
+                       expression_t *sub_addr = create_select(pos, addr, qualifiers, iter);
+                       sub_addr->base.source_position = *pos;
+                       sub_addr->select.implicit      = true;
+                       return find_create_select(pos, sub_addr, qualifiers, sub_compound,
+                                                 symbol);
+               }
+
+               if (iter_symbol == symbol) {
+                       return create_select(pos, addr, qualifiers, iter);
+               }
+       }
+
+       return NULL;
+}
+
 static void parse_compound_declarators(compound_t *compound,
                const declaration_specifiers_t *specifiers)
 {
@@ -5937,7 +5927,7 @@ static void parse_compound_declarators(compound_t *compound,
                        expression_t *size = parse_constant_expression();
 
                        type_t *type = make_bitfield_type(base_type, size,
-                                       &source_position, sym_anonymous);
+                                       &source_position, NULL);
 
                        attribute_t *attributes = parse_attributes(NULL);
                        if (attributes != NULL) {
@@ -6411,29 +6401,6 @@ type_t *revert_automatic_type_conversion(const expression_t *expression)
        }
 }
 
-static void check_deprecated(const source_position_t *source_position,
-                             const entity_t *entity)
-{
-       if (!warning.deprecated_declarations)
-               return;
-       if (!is_declaration(entity))
-               return;
-       if ((entity->declaration.modifiers & DM_DEPRECATED) == 0)
-               return;
-
-       char const *const prefix = get_entity_kind_name(entity->kind);
-       const char *deprecated_string
-                       = get_deprecated_string(entity->declaration.attributes);
-       if (deprecated_string != NULL) {
-               warningf(source_position, "%s '%Y' is deprecated (declared %P): \"%s\"",
-                                prefix, entity->base.symbol, &entity->base.source_position,
-                                deprecated_string);
-       } else {
-               warningf(source_position, "%s '%Y' is deprecated (declared %P)", prefix,
-                                entity->base.symbol, &entity->base.source_position);
-       }
-}
-
 static expression_t *parse_reference(void)
 {
        symbol_t *const symbol = token.v.symbol;
@@ -7356,29 +7323,26 @@ static expression_t *parse_alignof(void)
        return parse_typeprop(EXPR_ALIGNOF);
 }
 
-static expression_t *parse_select_expression(expression_t *compound)
+static expression_t *parse_select_expression(expression_t *addr)
 {
-       expression_t *select    = allocate_expression_zero(EXPR_SELECT);
-       select->select.compound = compound;
-
        assert(token.type == '.' || token.type == T_MINUSGREATER);
-       bool is_pointer = (token.type == T_MINUSGREATER);
+       bool select_left_arrow = (token.type == T_MINUSGREATER);
        next_token();
 
        if (token.type != T_IDENTIFIER) {
                parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
-               return select;
+               return create_invalid_expression();
        }
        symbol_t *symbol = token.v.symbol;
        next_token();
 
-       type_t *const orig_type = compound->base.type;
+       type_t *const orig_type = addr->base.type;
        type_t *const type      = skip_typeref(orig_type);
 
        type_t *type_left;
        bool    saw_error = false;
        if (is_type_pointer(type)) {
-               if (!is_pointer) {
+               if (!select_left_arrow) {
                        errorf(HERE,
                               "request for member '%Y' in something not a struct or union, but '%T'",
                               symbol, orig_type);
@@ -7386,58 +7350,41 @@ static expression_t *parse_select_expression(expression_t *compound)
                }
                type_left = skip_typeref(type->pointer.points_to);
        } else {
-               if (is_pointer && is_type_valid(type)) {
+               if (select_left_arrow && is_type_valid(type)) {
                        errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
                        saw_error = true;
                }
                type_left = type;
        }
 
-       entity_t *entry;
-       if (type_left->kind == TYPE_COMPOUND_STRUCT ||
-           type_left->kind == TYPE_COMPOUND_UNION) {
-               compound_t *compound = type_left->compound.compound;
+       if (type_left->kind != TYPE_COMPOUND_STRUCT &&
+           type_left->kind != TYPE_COMPOUND_UNION) {
 
-               if (!compound->complete) {
-                       errorf(HERE, "request for member '%Y' of incomplete type '%T'",
-                              symbol, type_left);
-                       goto create_error_entry;
-               }
-
-               entry = find_compound_entry(compound, symbol);
-               if (entry == NULL) {
-                       errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
-                       goto create_error_entry;
-               }
-       } else {
                if (is_type_valid(type_left) && !saw_error) {
                        errorf(HERE,
                               "request for member '%Y' in something not a struct or union, but '%T'",
                               symbol, type_left);
                }
-create_error_entry:
-               entry = create_error_entity(symbol, ENTITY_COMPOUND_MEMBER);
+               return create_invalid_expression();
        }
 
-       assert(is_declaration(entry));
-       select->select.compound_entry = entry;
-
-       check_deprecated(HERE, entry);
-
-       type_t *entry_type = entry->declaration.type;
-       type_t *res_type
-               = get_qualified_type(entry_type, type_left->base.qualifiers);
+       compound_t *compound = type_left->compound.compound;
+       if (!compound->complete) {
+               errorf(HERE, "request for member '%Y' in incomplete type '%T'",
+                      symbol, type_left);
+               return create_invalid_expression();
+       }
 
-       /* we always do the auto-type conversions; the & and sizeof parser contains
-        * code to revert this! */
-       select->base.type = automatic_type_conversion(res_type);
+       type_qualifiers_t  qualifiers = type_left->base.qualifiers;
+       expression_t      *result
+               = find_create_select(HERE, addr, qualifiers, compound, symbol);
 
-       type_t *skipped = skip_typeref(res_type);
-       if (skipped->kind == TYPE_BITFIELD) {
-               select->base.type = skipped->bitfield.base_type;
+       if (result == NULL) {
+               errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
+               return create_invalid_expression();
        }
 
-       return select;
+       return result;
 }
 
 static void check_call_argument(type_t          *expected_type,