1 // Copyright 2016 Klemens Morgenstern
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_DLL_DETAIL_DEMANGLING_MSVC_HPP_
8 #define BOOST_DLL_DETAIL_DEMANGLING_MSVC_HPP_
10 #include <boost/dll/detail/demangling/mangled_storage_base.hpp>
13 #include <boost/type_traits/is_const.hpp>
14 #include <boost/type_traits/is_volatile.hpp>
15 #include <boost/type_traits/is_lvalue_reference.hpp>
16 #include <boost/type_traits/is_rvalue_reference.hpp>
17 #include <boost/type_traits/function_traits.hpp>
18 #include <boost/type_traits/remove_reference.hpp>
20 #include <boost/spirit/home/x3.hpp>
22 namespace boost { namespace dll { namespace detail {
24 class mangled_storage_impl : public mangled_storage_base
29 template<typename Return, typename ...Args>
30 std::vector<std::string> get_func_params(dummy<Return(Args...)>) const
32 return {get_name<Args>()...};
34 template<typename Return, typename ...Args>
35 std::string get_return_type(dummy<Return(Args...)>) const
37 return get_name<Return>();
39 //function to remove preceding 'class ' or 'struct ' if the are given in this format.
41 inline static void trim_typename(std::string & val);
43 using ctor_sym = std::string;
44 using dtor_sym = std::string;
46 using mangled_storage_base::mangled_storage_base;
49 std::string get_variable(const std::string &name) const;
51 template<typename Func>
52 std::string get_function(const std::string &name) const;
54 template<typename Class, typename Func>
55 std::string get_mem_fn(const std::string &name) const;
57 template<typename Signature>
58 ctor_sym get_constructor() const;
60 template<typename Class>
61 dtor_sym get_destructor() const;
63 template<typename T> //overload, does not need to virtual.
64 std::string get_name() const
66 auto nm = mangled_storage_base::get_name<T>();
72 std::string get_vtable() const;
75 std::vector<std::string> get_related() const;
79 void mangled_storage_impl::trim_typename(std::string & val)
81 //remove preceding class or struct, because you might want to use a struct as class, et vice versa
85 static constexpr char class_ [7] = "class ";
86 static constexpr char struct_[8] = "struct ";
88 if (equal(begin(class_), end(class_)-1, val.begin())) //aklright, starts with 'class '
90 else if (val.size() >= 7)
91 if (equal(begin(struct_), end(struct_)-1, val.begin()))
99 namespace x3 = spirit::x3;
101 inline auto ptr_rule_impl(std::integral_constant<std::size_t, 32>)
103 return -((-x3::space) >> "__ptr32");
105 inline auto ptr_rule_impl(std::integral_constant<std::size_t, 64>)
107 return -((-x3::space) >> "__ptr64");
110 inline auto ptr_rule() {
111 return ptr_rule_impl(std::integral_constant<std::size_t, sizeof(std::size_t)*8>());
114 auto const visibility = ("public:" | x3::lit("protected:") | "private:");
115 auto const virtual_ = x3::space >> "virtual";
116 auto const static_ = x3::space >> x3::lit("static") ;
118 inline auto const_rule_impl(true_type ) {return x3::space >> "const";};
119 inline auto const_rule_impl(false_type) {return x3::eps;};
121 auto const_rule() {using t = is_const<typename remove_reference<T>::type>; return const_rule_impl(t());}
123 inline auto volatile_rule_impl(true_type ) {return x3::space >> "volatile";};
124 inline auto volatile_rule_impl(false_type) {return x3::eps;};
126 auto volatile_rule() {using t = is_volatile<typename remove_reference<T>::type>; return volatile_rule_impl(t());}
129 inline auto inv_const_rule_impl(true_type ) {return "const" >> x3::space ;};
130 inline auto inv_const_rule_impl(false_type) {return x3::eps;};
132 auto inv_const_rule() {using t = is_const<typename remove_reference<T>::type>; return inv_const_rule_impl(t());}
134 inline auto inv_volatile_rule_impl(true_type ) {return "volatile" >> x3::space;};
135 inline auto inv_volatile_rule_impl(false_type) {return x3::eps;};
137 auto inv_volatile_rule() {using t = is_volatile<typename remove_reference<T>::type>; return inv_volatile_rule_impl(t());}
140 inline auto reference_rule_impl(false_type, false_type) {return x3::eps;}
141 inline auto reference_rule_impl(true_type, false_type) {return x3::space >>"&" ;}
142 inline auto reference_rule_impl(false_type, true_type ) {return x3::space >>"&&" ;}
146 auto reference_rule() {using t_l = is_lvalue_reference<T>; using t_r = is_rvalue_reference<T>; return reference_rule_impl(t_l(), t_r());}
148 auto const class_ = ("class" | x3::lit("struct"));
150 //it takes a string, because it may be overloaded.
152 auto type_rule(const std::string & type_name)
156 return -(class_ >> x3::space)>> x3::string(type_name) >>
158 volatile_rule<T>() >>
159 reference_rule<T>() >>
163 inline auto type_rule<void>(const std::string &) { return x3::string("void"); };
165 auto const cdecl_ = "__cdecl" >> x3::space;
166 auto const stdcall = "__stdcall" >> x3::space;
167 #if defined(_WIN64)//seems to be necessary by msvc 14-x64
168 auto const thiscall = "__cdecl" >> x3::space;
170 auto const thiscall = "__thiscall" >> x3::space;
173 template<typename Return, typename Arg>
174 auto arg_list(const mangled_storage_impl & ms, Return (*)(Arg))
178 return type_rule<Arg>(ms.get_name<Arg>());
181 template<typename Return, typename First, typename Second, typename ...Args>
182 auto arg_list(const mangled_storage_impl & ms, Return (*)(First, Second, Args...))
185 using next_type = Return (*)(Second, Args...);
186 return type_rule<First>(ms.get_name<First>()) >> x3::char_(',') >> arg_list(ms, next_type());
189 template<typename Return>
190 auto arg_list(const mangled_storage_impl& /*ms*/, Return (*)())
192 return x3::string("void");
197 template<typename T> std::string mangled_storage_impl::get_variable(const std::string &name) const
200 using namespace boost;
202 namespace x3 = spirit::x3;
203 using namespace parser;
205 auto type_name = get_name<T>();
208 -(visibility >> static_ >> x3::space) >> //it may be a static class-member
209 parser::type_rule<T>(type_name) >> x3::space >>
212 auto predicate = [&](const mangled_storage_base::entry & e)
214 if (e.demangled == name)//maybe not mangled,
217 auto itr = e.demangled.begin();
218 auto end = e.demangled.end();
219 auto res = x3::parse(itr, end, matcher);
220 return res && (itr == end);
223 auto found = std::find_if(storage_.begin(), storage_.end(), predicate);
225 if (found != storage_.end())
226 return found->mangled;
231 template<typename Func> std::string mangled_storage_impl::get_function(const std::string &name) const
233 namespace x3 = spirit::x3;
234 using namespace parser;
235 using func_type = Func*;
236 using return_type = typename function_traits<Func>::result_type;
237 std::string return_type_name = get_name<return_type>();
241 -(visibility >> static_ >> x3::space) >> //it may be a static class-member, which does however not have the static attribute.
242 parser::type_rule<return_type>(return_type_name) >> x3::space >>
243 cdecl_ >> //cdecl declaration for methods. stdcall cannot be
244 name >> x3::lit('(') >> parser::arg_list(*this, func_type()) >> x3::lit(')') >> parser::ptr_rule();
247 auto predicate = [&](const mangled_storage_base::entry & e)
249 if (e.demangled == name)//maybe not mangled,
252 auto itr = e.demangled.begin();
253 auto end = e.demangled.end();
254 auto res = x3::parse(itr, end, matcher);
256 return res && (itr == end);
259 auto found = std::find_if(storage_.begin(), storage_.end(), predicate);
261 if (found != storage_.end())
262 return found->mangled;
268 template<typename Class, typename Func>
269 std::string mangled_storage_impl::get_mem_fn(const std::string &name) const
271 namespace x3 = spirit::x3;
272 using namespace parser;
273 using func_type = Func*;
274 using return_type = typename function_traits<Func>::result_type;
275 auto return_type_name = get_name<return_type>();
278 auto cname = get_name<Class>();
281 visibility >> -virtual_ >> x3::space >>
282 parser::type_rule<return_type>(return_type_name) >> x3::space >>
283 thiscall >> //cdecl declaration for methods. stdcall cannot be
284 cname >> "::" >> name >>
285 x3::lit('(') >> parser::arg_list(*this, func_type()) >> x3::lit(')') >>
286 inv_const_rule<Class>() >> inv_volatile_rule<Class>() >> parser::ptr_rule();
288 auto predicate = [&](const mangled_storage_base::entry & e)
290 auto itr = e.demangled.begin();
291 auto end = e.demangled.end();
292 auto res = x3::parse(itr, end, matcher);
294 return res && (itr == end);
297 auto found = std::find_if(storage_.begin(), storage_.end(), predicate);
299 if (found != storage_.end())
300 return found->mangled;
306 template<typename Signature>
307 auto mangled_storage_impl::get_constructor() const -> ctor_sym
309 namespace x3 = spirit::x3;
310 using namespace parser;
312 using func_type = Signature*;
315 std::string ctor_name; // = class_name + "::" + name;
316 std::string unscoped_cname; //the unscoped class-name
318 auto class_name = get_return_type(dummy<Signature>());
319 auto pos = class_name.rfind("::");
320 if (pos == std::string::npos)
322 ctor_name = class_name+ "::" + class_name ;
323 unscoped_cname = class_name;
327 unscoped_cname = class_name.substr(pos+2) ;
328 ctor_name = class_name+ "::" + unscoped_cname;
333 visibility >> x3::space >>
334 thiscall >> //cdecl declaration for methods. stdcall cannot be
336 x3::lit('(') >> parser::arg_list(*this, func_type()) >> x3::lit(')') >> parser::ptr_rule();
339 auto predicate = [&](const mangled_storage_base::entry & e)
341 auto itr = e.demangled.begin();
342 auto end = e.demangled.end();
343 auto res = x3::parse(itr, end, matcher);
345 return res && (itr == end);
348 auto f = std::find_if(storage_.begin(), storage_.end(), predicate);
350 if (f != storage_.end())
356 template<typename Class>
357 auto mangled_storage_impl::get_destructor() const -> dtor_sym
359 namespace x3 = spirit::x3;
360 using namespace parser;
361 std::string dtor_name; // = class_name + "::" + name;
362 std::string unscoped_cname; //the unscoped class-name
364 auto class_name = get_name<Class>();
365 auto pos = class_name.rfind("::");
366 if (pos == std::string::npos)
368 dtor_name = class_name+ "::~" + class_name + "(void)";
369 unscoped_cname = class_name;
373 unscoped_cname = class_name.substr(pos+2) ;
374 dtor_name = class_name+ "::~" + unscoped_cname + "(void)";
379 visibility >> -virtual_ >> x3::space >>
380 thiscall >> //cdecl declaration for methods. stdcall cannot be
381 dtor_name >> parser::ptr_rule();
384 auto predicate = [&](const mangled_storage_base::entry & e)
386 auto itr = e.demangled.begin();
387 auto end = e.demangled.end();
388 auto res = x3::parse(itr, end, matcher);
390 return res && (itr == end);
393 auto found = std::find_if(storage_.begin(), storage_.end(), predicate);
396 if (found != storage_.end())
397 return found->mangled;
403 std::string mangled_storage_impl::get_vtable() const
405 std::string id = "const " + get_name<T>() + "::`vftable'";
407 auto predicate = [&](const mangled_storage_base::entry & e)
409 return e.demangled == id;
412 auto found = std::find_if(storage_.begin(), storage_.end(), predicate);
415 if (found != storage_.end())
416 return found->mangled;
422 std::vector<std::string> mangled_storage_impl::get_related() const
424 std::vector<std::string> ret;
425 auto name = get_name<T>();
427 for (auto & c : storage_)
429 if (c.demangled.find(name) != std::string::npos)
430 ret.push_back(c.demangled);
441 #endif /* BOOST_DLL_DETAIL_DEMANGLING_MSVC_HPP_ */