fix
[cparser] / mangle.c
index b7d2bec..9cb8f81 100644 (file)
--- a/mangle.c
+++ b/mangle.c
@@ -1,6 +1,6 @@
 /*
  * This file is part of cparser.
- * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
+ * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -222,17 +222,32 @@ static void mangle_type(type_t *orig_type)
 
        case TYPE_BITFIELD:
                panic("no mangling for this type implemented yet");
-               break;
        }
        panic("invalid type encountered while mangling");
 }
 
+static void mangle_namespace(entity_t *entity)
+{
+       for (entity_t *e = entity->base.parent_entity; e != NULL;
+            e = e->base.parent_entity) {
+           /* TODO: we need something similar (or the same?) for classes */
+               if (e->kind == ENTITY_NAMESPACE) {
+                       mangle_namespace(e);
+                       print_name(e->base.symbol->string);
+                       return;
+               }
+       }
+}
+
 static void mangle_entity(entity_t *entity)
 {
        obstack_1grow(&obst, '_');
        obstack_1grow(&obst, 'Z');
 
-       /* TODO: mangle scope */
+       if (entity->base.parent_entity != NULL) {
+               obstack_1grow(&obst, 'N');
+               mangle_namespace(entity);
+       }
 
        print_name(entity->base.symbol->string);
 
@@ -262,12 +277,11 @@ ident *create_name_win32(entity_t *entity)
 
        assert(is_declaration(entity));
 
-       if (entity->declaration.modifiers & DM_DLLIMPORT) {
-               /* add prefix for imported symbols */
-               obstack_printf(o, "__imp_");
-       }
-
        if (entity->kind == ENTITY_FUNCTION) {
+               if (entity->declaration.modifiers & DM_DLLIMPORT)
+                       /* add prefix for imported symbols */
+                       obstack_printf(o, "__imp_");
+
                cc_kind_t cc = entity->declaration.type->function.calling_convention;
 
                /* calling convention prefix */
@@ -281,7 +295,7 @@ ident *create_name_win32(entity_t *entity)
 
                switch (entity->declaration.type->function.linkage) {
                        case LINKAGE_INVALID:
-                               break;
+                               panic("linkage type of function is invalid");
 
                        case LINKAGE_C:
                                obstack_printf(o, "%s", entity->base.symbol->string);
@@ -331,7 +345,9 @@ ident *create_name_linux_elf(entity_t *entity)
 
        if (entity->kind == ENTITY_FUNCTION) {
                switch (entity->declaration.type->function.linkage) {
-                       case LINKAGE_INVALID: break;
+                       case LINKAGE_INVALID:
+                               panic("linkage type of function is invalid");
+
                        case LINKAGE_C:       break;
                        case LINKAGE_CXX:     needs_mangling = true; break;
                }
@@ -353,6 +369,9 @@ ident *create_name_linux_elf(entity_t *entity)
  */
 ident *create_name_macho(entity_t *entity)
 {
+       if (entity->kind == ENTITY_FUNCTION && entity->declaration.type->function.linkage == LINKAGE_INVALID)
+               panic("linkage type of function is invalid");
+
        obstack_printf(&obst, "_%s", entity->base.symbol->string);
        return make_id_from_obst();
 }