Move local variable declaration closer to its use.
[cparser] / mangle.c
index 50075ce..1098307 100644 (file)
--- a/mangle.c
+++ b/mangle.c
@@ -31,8 +31,7 @@
 #include "lang_features.h"
 #include "adt/error.h"
 
-static ident          *id_underscore;
-static struct obstack  obst;
+static struct obstack obst;
 
 static void mangle_type(type_t *type);
 
@@ -71,6 +70,26 @@ static void mangle_pointer_type(const pointer_type_t *type)
        mangle_type(type->points_to);
 }
 
+static void mangle_parameters(const function_type_t *type)
+{
+       if (type->unspecified_parameters)
+               panic("can't mangle unspecified parameter types");
+       if (type->kr_style_parameters)
+               panic("can't mangle kr_style_parameters type");
+
+       const function_parameter_t *parameter = type->parameters;
+       if (parameter != NULL) {
+               for ( ; parameter != NULL; parameter = parameter->next) {
+                       mangle_type(parameter->type);
+               }
+               if (type->variadic) {
+                       obstack_1grow(&obst, 'z');
+               }
+       } else {
+               obstack_1grow(&obst, 'v');
+       }
+}
+
 static void mangle_function_type(const function_type_t *type)
 {
        obstack_1grow(&obst, 'F');
@@ -79,30 +98,19 @@ static void mangle_function_type(const function_type_t *type)
        }
 
        mangle_type(type->return_type);
-
-       function_parameter_t *parameter = type->parameters;
-       for ( ; parameter != NULL; parameter = parameter->next) {
-               mangle_type(parameter->type);
-       }
-       if (type->variadic) {
-               obstack_1grow(&obst, 'z');
-       }
-       if (type->unspecified_parameters)
-               panic("can't mangle unspecified parameter types");
-       if (type->kr_style_parameters)
-               panic("can't mangle kr_style_parameters type");
+       mangle_parameters(type);
 
        obstack_1grow(&obst, 'E');
 }
 
 static void mangle_qualifiers(type_qualifiers_t qualifiers)
 {
-       if (qualifiers & TYPE_QUALIFIER_CONST)
-               obstack_1grow(&obst, 'K');
-       if (qualifiers & TYPE_QUALIFIER_VOLATILE)
-               obstack_1grow(&obst, 'V');
        if (qualifiers & TYPE_QUALIFIER_RESTRICT)
                obstack_1grow(&obst, 'r');
+       if (qualifiers & TYPE_QUALIFIER_VOLATILE)
+               obstack_1grow(&obst, 'V');
+       if (qualifiers & TYPE_QUALIFIER_CONST)
+               obstack_1grow(&obst, 'K');
 
        /* handle MS extended qualifiers? */
 }
@@ -152,14 +160,23 @@ static void mangle_entity(entity_t *entity)
 
        /* TODO: mangle scope */
 
-       symbol_t *symbol = entity->base.symbol;
-       obstack_printf(&obst, "%u%s", strlen(symbol->string), symbol->string);
+       const char *name = entity->base.symbol->string;
+       obstack_printf(&obst, "%zu%s", strlen(name), name);
 
        if (entity->kind == ENTITY_FUNCTION) {
-               mangle_type(entity->declaration.type);
+               mangle_parameters(&entity->declaration.type->function);
        }
 }
 
+static ident *make_id_from_obst(void)
+{
+       size_t  size = obstack_object_size(&obst);
+       char   *str  = obstack_finish(&obst);
+       ident  *id   = new_id_from_chars(str, size);
+       obstack_free(&obst, str);
+       return id;
+}
+
 /**
  * Mangles an entity linker (ld) name for win32 usage.
  *
@@ -226,11 +243,7 @@ ident *create_name_win32(entity_t *entity)
                obstack_printf(o, "_%s", entity->base.symbol->string);
        }
 
-       size_t  size = obstack_object_size(o);
-       char   *str  = obstack_finish(o);
-       ident  *id   = new_id_from_chars(str, size);
-       obstack_free(o, str);
-       return id;
+       return make_id_from_obst();
 }
 
 /**
@@ -253,11 +266,7 @@ ident *create_name_linux_elf(entity_t *entity)
 
        if (needs_mangling) {
                mangle_entity(entity);
-               size_t  size = obstack_object_size(&obst);
-               char   *str  = obstack_finish(&obst);
-               ident  *id   = new_id_from_chars(str, size);
-               obstack_free(&obst, str);
-               return id;
+               return make_id_from_obst();
        }
 
        return new_id_from_str(entity->base.symbol->string);
@@ -271,14 +280,12 @@ ident *create_name_linux_elf(entity_t *entity)
  */
 ident *create_name_macho(entity_t *entity)
 {
-       ident *id = new_id_from_str(entity->base.symbol->string);
-       return id_mangle(id_underscore, id);
+       obstack_printf(&obst, "_%s", entity->base.symbol->string);
+       return make_id_from_obst();
 }
 
 void init_mangle(void)
 {
-       id_underscore = new_id_from_chars("_", 1);
-
        obstack_init(&obst);
 }