5 * Copyright (C) 2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "manes/manec.h"
24 #include "unmangler.h"
26 using namespace services
;
28 /* Main unmangling function */
29 string
unmangler::unmangle(const string
&name
) const {
31 if (strncmp(name
, "_Z", 2)) {
33 return get_argument(name
, index
);
41 while (index
< name
.length()) {
44 else if (arguments
== 1)
47 if (name
[index
] == 'K') {
52 if (name
[index
] == 'N') {
53 symbol
+= decode_name(name
, index
);
55 } else if (index
!= 2) {
56 symbol
+= get_argument(name
, index
);
63 symbol
+= (string
)") " + qualify
;
68 string
unmangler::decode_name(const string
&name
, int &index
) const {
69 if (name
[index
] != 'N')
77 for (;string::is_digit(name
[index
]); index
++)
78 length
= length
* 10 + name
[index
] - '0';
80 char *scope
= new char[length
+ 1];
81 strncpy(scope
, &name
[index
], length
);
84 symbol
+= (string
)"::";
86 symbol
+= (string
)scope
;
89 } while (name
[index
] != 'E');
93 /* Fundamental types */
94 const int type_number
= 14;
95 const char type_short
[] = { 'v', 'b', 'c', 'a', 'h', 's', 't', 'i', 'j', 'l', 'm', 'x', 'y', 'f', 'd' };
96 const char* type_long
[] = {
109 "unsigned long long",
114 /* Unmangle argument */
115 string
unmangler::get_argument(const string
&name
, int &index
) const {
117 /* FIXME: Probably wrong order */
118 if (name
[index
] == 'K') {
119 addons
+= (string
)" const";
122 if (name
[index
] == 'V') {
123 addons
+= (string
)" volatile";
126 if (name
[index
] == 'R') {
127 addons
+= (string
)" &";
129 } else if (name
[index
] == 'P') {
130 addons
+= (string
)" *";
133 if (name
[index
] == 'N')
134 return (string
)decode_name(name
, index
) + addons
;
135 else if (name
[index
] == 'F') {
137 string
func(get_argument(name
,index
));
138 func
+= (string
)" (*)(";
140 while (name
[index
] != 'E') {
142 func
+= (string
)", ";
144 func
+= get_argument(name
,index
);
150 for (int i
= 0; i
< type_number
; i
++)
151 if (type_short
[i
] == name
[index
])
152 return (string
)type_long
[i
] + addons
;
157 void unmangler::register_type() {
158 manes::manec::get()->register_type
<unmangler
>("unmangler", "service");