// Copyright 2016 Klemens Morgenstern // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_DLL_DETAIL_DEMANGLING_MSVC_HPP_ #define BOOST_DLL_DETAIL_DEMANGLING_MSVC_HPP_ #include #include #include #include #include #include #include #include #include #include namespace boost { namespace dll { namespace detail { class mangled_storage_impl : public mangled_storage_base { template struct dummy {}; template std::vector get_func_params(dummy) const { return {get_name()...}; } template std::string get_return_type(dummy) const { return get_name(); } //function to remove preceding 'class ' or 'struct ' if the are given in this format. inline static void trim_typename(std::string & val); public: using ctor_sym = std::string; using dtor_sym = std::string; using mangled_storage_base::mangled_storage_base; template std::string get_variable(const std::string &name) const; template std::string get_function(const std::string &name) const; template std::string get_mem_fn(const std::string &name) const; template ctor_sym get_constructor() const; template dtor_sym get_destructor() const; template //overload, does not need to virtual. std::string get_name() const { auto nm = mangled_storage_base::get_name(); trim_typename(nm); return nm; } template std::string get_vtable() const; template std::vector get_related() const; }; void mangled_storage_impl::trim_typename(std::string & val) { //remove preceding class or struct, because you might want to use a struct as class, et vice versa if (val.size() >= 6) { using namespace std; static constexpr char class_ [7] = "class "; static constexpr char struct_[8] = "struct "; if (equal(begin(class_), end(class_)-1, val.begin())) //aklright, starts with 'class ' val.erase(0, 6); else if (val.size() >= 7) if (equal(begin(struct_), end(struct_)-1, val.begin())) val.erase(0, 7); } } namespace parser { namespace x3 = spirit::x3; inline auto ptr_rule_impl(std::integral_constant) { return -((-x3::space) >> "__ptr32"); } inline auto ptr_rule_impl(std::integral_constant) { return -((-x3::space) >> "__ptr64"); } inline auto ptr_rule() { return ptr_rule_impl(std::integral_constant()); } auto const visibility = ("public:" | x3::lit("protected:") | "private:"); auto const virtual_ = x3::space >> "virtual"; auto const static_ = x3::space >> x3::lit("static") ; inline auto const_rule_impl(true_type ) {return x3::space >> "const";}; inline auto const_rule_impl(false_type) {return x3::eps;}; template auto const_rule() {using t = is_const::type>; return const_rule_impl(t());} inline auto volatile_rule_impl(true_type ) {return x3::space >> "volatile";}; inline auto volatile_rule_impl(false_type) {return x3::eps;}; template auto volatile_rule() {using t = is_volatile::type>; return volatile_rule_impl(t());} inline auto inv_const_rule_impl(true_type ) {return "const" >> x3::space ;}; inline auto inv_const_rule_impl(false_type) {return x3::eps;}; template auto inv_const_rule() {using t = is_const::type>; return inv_const_rule_impl(t());} inline auto inv_volatile_rule_impl(true_type ) {return "volatile" >> x3::space;}; inline auto inv_volatile_rule_impl(false_type) {return x3::eps;}; template auto inv_volatile_rule() {using t = is_volatile::type>; return inv_volatile_rule_impl(t());} inline auto reference_rule_impl(false_type, false_type) {return x3::eps;} inline auto reference_rule_impl(true_type, false_type) {return x3::space >>"&" ;} inline auto reference_rule_impl(false_type, true_type ) {return x3::space >>"&&" ;} template auto reference_rule() {using t_l = is_lvalue_reference; using t_r = is_rvalue_reference; return reference_rule_impl(t_l(), t_r());} auto const class_ = ("class" | x3::lit("struct")); //it takes a string, because it may be overloaded. template auto type_rule(const std::string & type_name) { using namespace std; return -(class_ >> x3::space)>> x3::string(type_name) >> const_rule() >> volatile_rule() >> reference_rule() >> ptr_rule(); } template<> inline auto type_rule(const std::string &) { return x3::string("void"); }; auto const cdecl_ = "__cdecl" >> x3::space; auto const stdcall = "__stdcall" >> x3::space; #if defined(_WIN64)//seems to be necessary by msvc 14-x64 auto const thiscall = "__cdecl" >> x3::space; #else auto const thiscall = "__thiscall" >> x3::space; #endif template auto arg_list(const mangled_storage_impl & ms, Return (*)(Arg)) { using namespace std; return type_rule(ms.get_name()); } template auto arg_list(const mangled_storage_impl & ms, Return (*)(First, Second, Args...)) { using next_type = Return (*)(Second, Args...); return type_rule(ms.get_name()) >> x3::char_(',') >> arg_list(ms, next_type()); } template auto arg_list(const mangled_storage_impl& /*ms*/, Return (*)()) { return x3::string("void"); } } template std::string mangled_storage_impl::get_variable(const std::string &name) const { using namespace std; using namespace boost; namespace x3 = spirit::x3; using namespace parser; auto type_name = get_name(); auto matcher = -(visibility >> static_ >> x3::space) >> //it may be a static class-member parser::type_rule(type_name) >> x3::space >> name; auto predicate = [&](const mangled_storage_base::entry & e) { if (e.demangled == name)//maybe not mangled, return true; auto itr = e.demangled.begin(); auto end = e.demangled.end(); auto res = x3::parse(itr, end, matcher); return res && (itr == end); }; auto found = std::find_if(storage_.begin(), storage_.end(), predicate); if (found != storage_.end()) return found->mangled; else return ""; } template std::string mangled_storage_impl::get_function(const std::string &name) const { namespace x3 = spirit::x3; using namespace parser; using func_type = Func*; using return_type = typename function_traits::result_type; std::string return_type_name = get_name(); auto matcher = -(visibility >> static_ >> x3::space) >> //it may be a static class-member, which does however not have the static attribute. parser::type_rule(return_type_name) >> x3::space >> cdecl_ >> //cdecl declaration for methods. stdcall cannot be name >> x3::lit('(') >> parser::arg_list(*this, func_type()) >> x3::lit(')') >> parser::ptr_rule(); auto predicate = [&](const mangled_storage_base::entry & e) { if (e.demangled == name)//maybe not mangled, return true; auto itr = e.demangled.begin(); auto end = e.demangled.end(); auto res = x3::parse(itr, end, matcher); return res && (itr == end); }; auto found = std::find_if(storage_.begin(), storage_.end(), predicate); if (found != storage_.end()) return found->mangled; else return ""; } template std::string mangled_storage_impl::get_mem_fn(const std::string &name) const { namespace x3 = spirit::x3; using namespace parser; using func_type = Func*; using return_type = typename function_traits::result_type; auto return_type_name = get_name(); auto cname = get_name(); auto matcher = visibility >> -virtual_ >> x3::space >> parser::type_rule(return_type_name) >> x3::space >> thiscall >> //cdecl declaration for methods. stdcall cannot be cname >> "::" >> name >> x3::lit('(') >> parser::arg_list(*this, func_type()) >> x3::lit(')') >> inv_const_rule() >> inv_volatile_rule() >> parser::ptr_rule(); auto predicate = [&](const mangled_storage_base::entry & e) { auto itr = e.demangled.begin(); auto end = e.demangled.end(); auto res = x3::parse(itr, end, matcher); return res && (itr == end); }; auto found = std::find_if(storage_.begin(), storage_.end(), predicate); if (found != storage_.end()) return found->mangled; else return ""; } template auto mangled_storage_impl::get_constructor() const -> ctor_sym { namespace x3 = spirit::x3; using namespace parser; using func_type = Signature*; std::string ctor_name; // = class_name + "::" + name; std::string unscoped_cname; //the unscoped class-name { auto class_name = get_return_type(dummy()); auto pos = class_name.rfind("::"); if (pos == std::string::npos) { ctor_name = class_name+ "::" + class_name ; unscoped_cname = class_name; } else { unscoped_cname = class_name.substr(pos+2) ; ctor_name = class_name+ "::" + unscoped_cname; } } auto matcher = visibility >> x3::space >> thiscall >> //cdecl declaration for methods. stdcall cannot be ctor_name >> x3::lit('(') >> parser::arg_list(*this, func_type()) >> x3::lit(')') >> parser::ptr_rule(); auto predicate = [&](const mangled_storage_base::entry & e) { auto itr = e.demangled.begin(); auto end = e.demangled.end(); auto res = x3::parse(itr, end, matcher); return res && (itr == end); }; auto f = std::find_if(storage_.begin(), storage_.end(), predicate); if (f != storage_.end()) return f->mangled; else return ""; } template auto mangled_storage_impl::get_destructor() const -> dtor_sym { namespace x3 = spirit::x3; using namespace parser; std::string dtor_name; // = class_name + "::" + name; std::string unscoped_cname; //the unscoped class-name { auto class_name = get_name(); auto pos = class_name.rfind("::"); if (pos == std::string::npos) { dtor_name = class_name+ "::~" + class_name + "(void)"; unscoped_cname = class_name; } else { unscoped_cname = class_name.substr(pos+2) ; dtor_name = class_name+ "::~" + unscoped_cname + "(void)"; } } auto matcher = visibility >> -virtual_ >> x3::space >> thiscall >> //cdecl declaration for methods. stdcall cannot be dtor_name >> parser::ptr_rule(); auto predicate = [&](const mangled_storage_base::entry & e) { auto itr = e.demangled.begin(); auto end = e.demangled.end(); auto res = x3::parse(itr, end, matcher); return res && (itr == end); }; auto found = std::find_if(storage_.begin(), storage_.end(), predicate); if (found != storage_.end()) return found->mangled; else return ""; } template std::string mangled_storage_impl::get_vtable() const { std::string id = "const " + get_name() + "::`vftable'"; auto predicate = [&](const mangled_storage_base::entry & e) { return e.demangled == id; }; auto found = std::find_if(storage_.begin(), storage_.end(), predicate); if (found != storage_.end()) return found->mangled; else return ""; } template std::vector mangled_storage_impl::get_related() const { std::vector ret; auto name = get_name(); for (auto & c : storage_) { if (c.demangled.find(name) != std::string::npos) ret.push_back(c.demangled); } return ret; } }}} #endif /* BOOST_DLL_DETAIL_DEMANGLING_MSVC_HPP_ */