fd2af1a4006274070c84d87f7ecbce6f25d1b4e3
[libfirm] / ir / ident / mangle.c
1 /*
2  * Copyright (C) 1995-2010 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Methods to manipulate names.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  */
25 #include "config.h"
26
27 #include <stdio.h>
28
29 #include "ident_t.h"
30 #include "obst.h"
31 #include "irprintf.h"
32
33 /* Make types visible to allow most efficient access */
34 #include "entity_t.h"
35 #include "type_t.h"
36 #include "tpop_t.h"
37
38 /** An obstack used for temporary space */
39 static struct obstack mangle_obst;
40
41 /** returned a mangled type name, currently no mangling */
42 static inline ident *mangle_type(const ir_type *tp)
43 {
44         assert(tp->kind == k_type);
45         return tp->name;
46 }
47
48 ident *id_mangle_entity(const ir_entity *ent)
49 {
50         ident *type_id;
51         char *cp;
52         int len;
53         ident *res;
54
55         type_id = mangle_type(ent->owner);
56         obstack_grow(&mangle_obst, get_id_str(type_id), get_id_strlen(type_id));
57         obstack_1grow(&mangle_obst,'_');
58         obstack_grow(&mangle_obst, get_id_str(ent->name), get_id_strlen(ent->name));
59         len = obstack_object_size(&mangle_obst);
60         cp = (char*)obstack_finish(&mangle_obst);
61         res = new_id_from_chars(cp, len);
62         obstack_free(&mangle_obst, cp);
63         return res;
64 }
65
66
67 /* Returns a new ident that represents 'firstscnd'. */
68 ident *id_mangle(ident *first, ident *scnd)
69 {
70         char *cp;
71         int len;
72         ident *res;
73
74         obstack_grow(&mangle_obst, get_id_str(first), get_id_strlen(first));
75         obstack_grow(&mangle_obst, get_id_str(scnd), get_id_strlen(scnd));
76         len = obstack_object_size(&mangle_obst);
77         cp = (char*)obstack_finish(&mangle_obst);
78         res = new_id_from_chars(cp, len);
79         obstack_free(&mangle_obst, cp);
80         return res;
81 }
82
83 /** Returns a new ident that represents 'prefixscndsuffix'. */
84 ident *id_mangle3(const char *prefix, ident *scnd, const char *suffix)
85 {
86         char *cp;
87         int len;
88         ident *res;
89
90         obstack_grow(&mangle_obst, prefix, strlen(prefix));
91         obstack_grow(&mangle_obst, get_id_str(scnd), get_id_strlen(scnd));
92         obstack_grow(&mangle_obst, suffix, strlen(suffix));
93         len = obstack_object_size(&mangle_obst);
94         cp  = (char*)obstack_finish(&mangle_obst);
95         res = new_id_from_chars(cp, len);
96         obstack_free(&mangle_obst, cp);
97         return res;
98 }
99
100 /** Returns a new ident that represents first<c>scnd. */
101 static ident *id_mangle_3(ident *first, char c, ident *scnd)
102 {
103         char *cp;
104         int len;
105         ident *res;
106
107         obstack_grow(&mangle_obst, get_id_str(first), get_id_strlen(first));
108         obstack_1grow(&mangle_obst, c);
109         obstack_grow(&mangle_obst,get_id_str(scnd),get_id_strlen(scnd));
110         len = obstack_object_size(&mangle_obst);
111         cp = (char*)obstack_finish(&mangle_obst);
112         res = new_id_from_chars(cp, len);
113         obstack_free(&mangle_obst, cp);
114         return res;
115 }
116
117 /* Returns a new ident that represents first_scnd. */
118 ident *id_mangle_u(ident *first, ident* scnd)
119 {
120         return id_mangle_3(first, '_', scnd);
121 }
122
123 /* Returns a new ident that represents first.scnd. */
124 ident *id_mangle_dot(ident *first, ident *scnd)
125 {
126         return id_mangle_3(first, '.', scnd);
127 }
128
129 void firm_init_mangle(void)
130 {
131         obstack_init(&mangle_obst);
132 }
133
134 void firm_finish_mangle(void)
135 {
136         obstack_free(&mangle_obst, NULL);
137 }