ast2firm: Factorise code to convert a value to its storage type.
[cparser] / attribute.c
index a7deee5..3ce2644 100644 (file)
 #include "type_t.h"
 
 static const char *const attribute_names[ATTRIBUTE_LAST+1] = {
-       [ATTRIBUTE_GNU_CONST]                  = "const",
-       [ATTRIBUTE_GNU_VOLATILE]               = "volatile",
+       [ATTRIBUTE_GNU_ALIAS]                  = "alias",
+       [ATTRIBUTE_GNU_ALIGNED]                = "aligned",
+       [ATTRIBUTE_GNU_ALWAYS_INLINE]          = "always_inline",
        [ATTRIBUTE_GNU_CDECL]                  = "cdecl",
-       [ATTRIBUTE_GNU_STDCALL]                = "stdcall",
-       [ATTRIBUTE_GNU_FASTCALL]               = "fastcall",
+       [ATTRIBUTE_GNU_COMMON]                 = "common",
+       [ATTRIBUTE_GNU_CONST]                  = "const",
+       [ATTRIBUTE_GNU_CONSTRUCTOR]            = "constructor",
        [ATTRIBUTE_GNU_DEPRECATED]             = "deprecated",
+       [ATTRIBUTE_GNU_DESTRUCTOR]             = "destructor",
+       [ATTRIBUTE_GNU_DLLEXPORT]              = "dllexport",
+       [ATTRIBUTE_GNU_DLLIMPORT]              = "dllimport",
+       [ATTRIBUTE_GNU_EIGTHBIT_DATA]          = "eightbit_data",
+       [ATTRIBUTE_GNU_EXTERNALLY_VISIBLE]     = "externally_visible",
+       [ATTRIBUTE_GNU_FAR]                    = "far",
+       [ATTRIBUTE_GNU_FASTCALL]               = "fastcall",
+       [ATTRIBUTE_GNU_FLATTEN]                = "flatten",
+       [ATTRIBUTE_GNU_FORMAT_ARG]             = "format_arg",
+       [ATTRIBUTE_GNU_FORMAT]                 = "format",
+       [ATTRIBUTE_GNU_FUNCTION_VECTOR]        = "function_vector",
+       [ATTRIBUTE_GNU_GCC_STRUCT]             = "gcc_struct",
+       [ATTRIBUTE_GNU_INTERRUPT_HANDLER]      = "interrupt_handler",
+       [ATTRIBUTE_GNU_INTERRUPT]              = "interrupt",
+       [ATTRIBUTE_GNU_LEAF]                   = "leaf",
+       [ATTRIBUTE_GNU_LONGCALL]               = "longcall",
+       [ATTRIBUTE_GNU_LONG_CALL]              = "long_call",
+       [ATTRIBUTE_GNU_MALLOC]                 = "malloc",
+       [ATTRIBUTE_GNU_MAY_ALIAS]              = "may_alias",
+       [ATTRIBUTE_GNU_MODEL]                  = "model",
+       [ATTRIBUTE_GNU_MODE]                   = "mode",
+       [ATTRIBUTE_GNU_MS_STRUCT]              = "ms_struct",
+       [ATTRIBUTE_GNU_NAKED]                  = "naked",
+       [ATTRIBUTE_GNU_NEAR]                   = "near",
+       [ATTRIBUTE_GNU_NESTING]                = "nesting",
+       [ATTRIBUTE_GNU_NMI_HANDLER]            = "nmi_handler",
+       [ATTRIBUTE_GNU_NOCOMMON]               = "nocommon",
        [ATTRIBUTE_GNU_NOINLINE]               = "noinline",
-       [ATTRIBUTE_GNU_RETURNS_TWICE]          = "returns_twice",
+       [ATTRIBUTE_GNU_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
+       [ATTRIBUTE_GNU_NONNULL]                = "nonnull",
        [ATTRIBUTE_GNU_NORETURN]               = "noreturn",
-       [ATTRIBUTE_GNU_NAKED]                  = "naked",
-       [ATTRIBUTE_GNU_PURE]                   = "pure",
-       [ATTRIBUTE_GNU_ALWAYS_INLINE]          = "always_inline",
-       [ATTRIBUTE_GNU_MALLOC]                 = "malloc",
-       [ATTRIBUTE_GNU_WEAK]                   = "weak",
-       [ATTRIBUTE_GNU_CONSTRUCTOR]            = "constructor",
-       [ATTRIBUTE_GNU_DESTRUCTOR]             = "destructor",
        [ATTRIBUTE_GNU_NOTHROW]                = "nothrow",
-       [ATTRIBUTE_GNU_TRANSPARENT_UNION]      = "transparent_union",
-       [ATTRIBUTE_GNU_COMMON]                 = "common",
-       [ATTRIBUTE_GNU_NOCOMMON]               = "nocommon",
+       [ATTRIBUTE_GNU_NOTSHARED]              = "notshared",
        [ATTRIBUTE_GNU_PACKED]                 = "packed",
+       [ATTRIBUTE_GNU_PURE]                   = "pure",
+       [ATTRIBUTE_GNU_REGPARM]                = "regparm",
+       [ATTRIBUTE_GNU_RETURNS_TWICE]          = "returns_twice",
+       [ATTRIBUTE_GNU_SAVEALL]                = "saveall",
+       [ATTRIBUTE_GNU_SECTION]                = "section",
+       [ATTRIBUTE_GNU_SENTINEL]               = "sentinel",
        [ATTRIBUTE_GNU_SHARED]                 = "shared",
-       [ATTRIBUTE_GNU_NOTSHARED]              = "notshared",
-       [ATTRIBUTE_GNU_USED]                   = "used",
-       [ATTRIBUTE_GNU_UNUSED]                 = "unused",
-       [ATTRIBUTE_GNU_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
-       [ATTRIBUTE_GNU_WARN_UNUSED_RESULT]     = "warn_unused_result",
-       [ATTRIBUTE_GNU_LONGCALL]               = "longcall",
        [ATTRIBUTE_GNU_SHORTCALL]              = "shortcall",
-       [ATTRIBUTE_GNU_LONG_CALL]              = "long_call",
        [ATTRIBUTE_GNU_SHORT_CALL]             = "short_call",
-       [ATTRIBUTE_GNU_FUNCTION_VECTOR]        = "function_vector",
-       [ATTRIBUTE_GNU_INTERRUPT]              = "interrupt",
-       [ATTRIBUTE_GNU_INTERRUPT_HANDLER]      = "interrupt_handler",
-       [ATTRIBUTE_GNU_NMI_HANDLER]            = "nmi_handler",
-       [ATTRIBUTE_GNU_NESTING]                = "nesting",
-       [ATTRIBUTE_GNU_NEAR]                   = "near",
-       [ATTRIBUTE_GNU_FAR]                    = "far",
        [ATTRIBUTE_GNU_SIGNAL]                 = "signal",
-       [ATTRIBUTE_GNU_EIGTHBIT_DATA]          = "eightbit_data",
-       [ATTRIBUTE_GNU_TINY_DATA]              = "tiny_data",
-       [ATTRIBUTE_GNU_SAVEALL]                = "saveall",
-       [ATTRIBUTE_GNU_FLATTEN]                = "flatten",
+       [ATTRIBUTE_GNU_SP_SWITCH]              = "sp_switch",
        [ATTRIBUTE_GNU_SSEREGPARM]             = "sseregparm",
-       [ATTRIBUTE_GNU_EXTERNALLY_VISIBLE]     = "externally_visible",
-       [ATTRIBUTE_GNU_MAY_ALIAS]              = "may_alias",
-       [ATTRIBUTE_GNU_MS_STRUCT]              = "ms_struct",
-       [ATTRIBUTE_GNU_GCC_STRUCT]             = "gcc_struct",
-       [ATTRIBUTE_GNU_DLLIMPORT]              = "dllimport",
-       [ATTRIBUTE_GNU_DLLEXPORT]              = "dllexport",
-       [ATTRIBUTE_GNU_ALIGNED]                = "aligned",
-       [ATTRIBUTE_GNU_ALIAS]                  = "alias",
-       [ATTRIBUTE_GNU_SECTION]                = "section",
-       [ATTRIBUTE_GNU_FORMAT]                 = "format",
-       [ATTRIBUTE_GNU_FORMAT_ARG]             = "format_arg",
-       [ATTRIBUTE_GNU_WEAKREF]                = "weakref",
-       [ATTRIBUTE_GNU_NONNULL]                = "nonnull",
+       [ATTRIBUTE_GNU_STDCALL]                = "stdcall",
+       [ATTRIBUTE_GNU_TINY_DATA]              = "tiny_data",
        [ATTRIBUTE_GNU_TLS_MODEL]              = "tls_model",
-       [ATTRIBUTE_GNU_VISIBILITY]             = "visibility",
-       [ATTRIBUTE_GNU_REGPARM]                = "regparm",
-       [ATTRIBUTE_GNU_MODE]                   = "mode",
-       [ATTRIBUTE_GNU_MODEL]                  = "model",
+       [ATTRIBUTE_GNU_TRANSPARENT_UNION]      = "transparent_union",
        [ATTRIBUTE_GNU_TRAP_EXIT]              = "trap_exit",
-       [ATTRIBUTE_GNU_SP_SWITCH]              = "sp_switch",
-       [ATTRIBUTE_GNU_SENTINEL]               = "sentinel",
+       [ATTRIBUTE_GNU_UNUSED]                 = "unused",
+       [ATTRIBUTE_GNU_USED]                   = "used",
+       [ATTRIBUTE_GNU_VISIBILITY]             = "visibility",
+       [ATTRIBUTE_GNU_VOLATILE]               = "volatile",
+       [ATTRIBUTE_GNU_WARN_UNUSED_RESULT]     = "warn_unused_result",
+       [ATTRIBUTE_GNU_WEAKREF]                = "weakref",
+       [ATTRIBUTE_GNU_WEAK]                   = "weak",
 
        [ATTRIBUTE_MS_ALIGN]                   = "align",
        [ATTRIBUTE_MS_ALLOCATE]                = "allocate",
-       [ATTRIBUTE_MS_DLLIMPORT]               = "dllimport",
+       [ATTRIBUTE_MS_DEPRECATED]              = "deprecated",
        [ATTRIBUTE_MS_DLLEXPORT]               = "dllexport",
+       [ATTRIBUTE_MS_DLLIMPORT]               = "dllimport",
        [ATTRIBUTE_MS_NAKED]                   = "naked",
+       [ATTRIBUTE_MS_NOALIAS]                 = "noalias",
        [ATTRIBUTE_MS_NOINLINE]                = "noinline",
-       [ATTRIBUTE_MS_RETURNS_TWICE]           = "returns_twice",
        [ATTRIBUTE_MS_NORETURN]                = "noreturn",
        [ATTRIBUTE_MS_NOTHROW]                 = "nothrow",
        [ATTRIBUTE_MS_NOVTABLE]                = "novtable",
        [ATTRIBUTE_MS_PROPERTY]                = "property",
+       [ATTRIBUTE_MS_RESTRICT]                = "restrict",
+       [ATTRIBUTE_MS_RETURNS_TWICE]           = "returns_twice",
        [ATTRIBUTE_MS_SELECTANY]               = "selectany",
        [ATTRIBUTE_MS_THREAD]                  = "thread",
        [ATTRIBUTE_MS_UUID]                    = "uuid",
-       [ATTRIBUTE_MS_DEPRECATED]              = "deprecated",
-       [ATTRIBUTE_MS_RESTRICT]                = "restrict",
-       [ATTRIBUTE_MS_NOALIAS]                 = "noalias",
 };
 
 const char *get_attribute_name(attribute_kind_t kind)
@@ -131,28 +132,27 @@ type_t *handle_attribute_mode(const attribute_t *attribute, type_t *orig_type)
         * specific modes (according to gcc philosophy that is...) */
        attribute_argument_t *arg = attribute->a.arguments;
        if (arg == NULL) {
-               errorf(&attribute->source_position,
-                      "__attribute__((mode(X))) misses argument");
+               errorf(&attribute->pos, "__attribute__((mode(X))) misses argument");
                return orig_type;
        }
 
        const char         *symbol_str = arg->v.symbol->string;
        bool                sign       = is_type_signed(type);
        atomic_type_kind_t  akind;
-       if (strcmp_underscore("QI",   symbol_str) == 0 ||
-           strcmp_underscore("byte", symbol_str) == 0) {
+       if (streq_underscore("QI",   symbol_str) ||
+           streq_underscore("byte", symbol_str)) {
                akind = sign ? ATOMIC_TYPE_CHAR : ATOMIC_TYPE_UCHAR;
-       } else if (strcmp_underscore("HI", symbol_str) == 0) {
+       } else if (streq_underscore("HI", symbol_str)) {
                akind = sign ? ATOMIC_TYPE_SHORT : ATOMIC_TYPE_USHORT;
-       } else if (strcmp_underscore("SI",      symbol_str) == 0
-               || strcmp_underscore("word",    symbol_str) == 0
-               || strcmp_underscore("pointer", symbol_str) == 0) {
+       } else if (streq_underscore("SI",      symbol_str)
+               || streq_underscore("word",    symbol_str)
+               || streq_underscore("pointer", symbol_str)) {
                akind = sign ? ATOMIC_TYPE_INT : ATOMIC_TYPE_UINT;
-       } else if (strcmp_underscore("DI", symbol_str) == 0) {
+       } else if (streq_underscore("DI", symbol_str)) {
                akind = sign ? ATOMIC_TYPE_LONGLONG : ATOMIC_TYPE_ULONGLONG;
        } else {
-               source_position_t const *const pos = &attribute->source_position;
-               warningf(WARN_OTHER, pos, "ignoring unknown mode '%s'", symbol_str);
+               warningf(WARN_OTHER, &attribute->pos, "ignoring unknown mode '%s'",
+                        symbol_str);
                return orig_type;
        }
 
@@ -161,12 +161,11 @@ type_t *handle_attribute_mode(const attribute_t *attribute, type_t *orig_type)
                copy->atomic.akind = akind;
                return identify_new_type(copy);
        } else if (is_type_pointer(type)) {
-               source_position_t const *const pos = &attribute->source_position;
-               warningf(WARN_OTHER, pos, "__attribute__((mode)) on pointers not implemented yet (ignored)");
+               warningf(WARN_OTHER, &attribute->pos, "__attribute__((mode)) on pointers not implemented yet (ignored)");
                return type;
        }
 
-       errorf(&attribute->source_position,
+       errorf(&attribute->pos,
               "__attribute__((mode)) only allowed on integer, enum or pointer type");
        return orig_type;
 }
@@ -187,20 +186,16 @@ static void handle_attribute_aligned(const attribute_t *attribute,
        }
 
        if (!is_po2(alignment)) {
-               errorf(&attribute->source_position,
-                          "alignment must be a power of 2 but is %d\n",
-                          alignment);
+               errorf(&attribute->pos, "alignment must be a power of 2 but is %d", alignment);
                return;
        }
        if (alignment <= 0) {
-               errorf(&attribute->source_position,
-                          "alignment must be bigger than 0 but is %d\n",
-                          alignment);
+               errorf(&attribute->pos, "alignment must be bigger than 0 but is %d", alignment);
                return;
        }
 
        switch (entity->kind) {
-       DECLARATION_KIND_CASES
+       case DECLARATION_KIND_CASES:
                entity->declaration.alignment = alignment;
        case ENTITY_TYPEDEF:
                entity->typedefe.alignment = alignment;
@@ -211,14 +206,11 @@ static void handle_attribute_aligned(const attribute_t *attribute,
                        entity->compound.alignment = alignment;
                }
                break;
-       default: {
-               source_position_t const *const pos  = &attribute->source_position;
-               char              const *const what = get_entity_kind_name(entity->kind);
-               symbol_t          const *const sym  = entity->base.symbol;
-               warningf(WARN_OTHER, pos, "alignment attribute specification on %s '%S' ignored", what, sym);
+
+       default:
+               warningf(WARN_OTHER, &attribute->pos, "alignment attribute specification on '%N' ignored", entity);
                break;
        }
-       }
 }
 
 static const char *get_argument_string(const attribute_argument_t *argument)
@@ -230,7 +222,7 @@ static const char *get_argument_string(const attribute_argument_t *argument)
        expression_t *expression = argument->v.expression;
        if (expression->kind != EXPR_STRING_LITERAL)
                return NULL;
-       return expression->literal.value.begin;
+       return expression->string_literal.value.begin;
 }
 
 static void handle_attribute_visibility(const attribute_t *attribute,
@@ -240,19 +232,19 @@ static void handle_attribute_visibility(const attribute_t *attribute,
         * specific modes (according to gcc philosophy that is...) */
        attribute_argument_t *arg = attribute->a.arguments;
        if (arg == NULL) {
-               errorf(&attribute->source_position,
+               errorf(&attribute->pos,
                       "__attribute__((visibility(X))) misses argument");
                return;
        }
        const char *string = get_argument_string(arg);
        if (string == NULL) {
-               errorf(&attribute->source_position,
+               errorf(&attribute->pos,
                       "__attribute__((visibility(X))) argument is not a string");
                return;
        }
        elf_visibility_tag_t visibility = get_elf_visibility_from_string(string);
        if (visibility == ELF_VISIBILITY_ERROR) {
-               errorf(&attribute->source_position,
+               errorf(&attribute->pos,
                       "unknown visibility type '%s'", string);
                return;
        }
@@ -264,14 +256,11 @@ static void handle_attribute_visibility(const attribute_t *attribute,
        case ENTITY_FUNCTION:
                entity->function.elf_visibility = visibility;
                break;
-       default: {
-               source_position_t const *const pos  = &attribute->source_position;
-               char              const *const what = get_entity_kind_name(entity->kind);
-               symbol_t          const *const sym  = entity->base.symbol;
-               warningf(WARN_OTHER, pos, "visibility attribute specification on %s '%S' ignored", what, sym);
+
+       default:
+               warningf(WARN_OTHER, &attribute->pos, "visibility attribute specification on '%N' ignored", entity);
                break;
        }
-       }
 }
 
 static void warn_arguments(const attribute_t *attribute)
@@ -279,8 +268,8 @@ static void warn_arguments(const attribute_t *attribute)
        if (attribute->a.arguments == NULL)
                return;
 
-       source_position_t const *const pos  = &attribute->source_position;
-       char              const *const what = get_attribute_name(attribute->kind);
+       position_t const *const pos  = &attribute->pos;
+       char       const *const what = get_attribute_name(attribute->kind);
        warningf(WARN_OTHER, pos, "attribute '%s' needs no arguments", what);
 }
 
@@ -289,10 +278,7 @@ static void handle_attribute_packed_e(const attribute_t *attribute,
 {
 #if 0
        if (entity->kind != ENTITY_STRUCT) {
-               source_position_t const *const pos  = &attribute->source_position;
-               char              const *const what = get_entity_kind_name(entity->kind);
-               symbol_t          const *const sym  = entity->base.symbol;
-               warningf(WARN_OTHER, pos, "packed attribute on %s '%S' ignored", what, sym);
+               warningf(WARN_OTHER, &attribute->pos, "packed attribute on '%N' ignored", entity);
                return;
        }
 #endif
@@ -304,7 +290,7 @@ static void handle_attribute_packed_e(const attribute_t *attribute,
 static void handle_attribute_packed(const attribute_t *attribute, type_t *type)
 {
        if (type->kind != TYPE_COMPOUND_STRUCT) {
-               source_position_t const *const pos  = &attribute->source_position;
+               position_t const *const pos  = &attribute->pos;
                warningf(WARN_OTHER, pos, "packed attribute on type '%T' ignored", type);
                return;
        }
@@ -319,8 +305,7 @@ static void handle_attribute_asm(const attribute_t *attribute,
        assert (argument->kind == ATTRIBUTE_ARGUMENT_EXPRESSION);
        expression_t *expression = argument->v.expression;
        if (expression->kind != EXPR_STRING_LITERAL)
-               errorf(&attribute->source_position,
-                      "Invalid asm attribute expression");
+               errorf(&attribute->pos, "Invalid asm attribute expression");
        symbol_t *sym = symbol_table_insert(expression->string_literal.value.begin);
        entity->function.actual_name = sym;
        assert(argument->next == NULL);
@@ -359,6 +344,7 @@ void handle_entity_attributes(const attribute_t *attributes, entity_t *entity)
                case ATTRIBUTE_GNU_DLLIMPORT:     modifiers |= DM_DLLIMPORT; break;
                case ATTRIBUTE_GNU_DLLEXPORT:     modifiers |= DM_DLLEXPORT; break;
                case ATTRIBUTE_GNU_WEAK:          modifiers |= DM_WEAK; break;
+               case ATTRIBUTE_GNU_LEAF:          modifiers |= DM_LEAF; break;
 
                case ATTRIBUTE_MS_DLLIMPORT:     modifiers |= DM_DLLIMPORT; break;
                case ATTRIBUTE_MS_DLLEXPORT:     modifiers |= DM_DLLEXPORT; break;
@@ -520,7 +506,7 @@ static bool attribute_argument_equal(const attribute_argument_t *arg1,
                /* TODO */
                return false;
        }
-       panic("Unknown argument type found");
+       panic("unknown argument type");
 }
 
 static bool attribute_arguments_equal(const attribute_argument_t *args1,