msvc.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // Copyright 2016 Klemens Morgenstern
  2. //
  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)
  6. #ifndef BOOST_DLL_DETAIL_DEMANGLING_MSVC_HPP_
  7. #define BOOST_DLL_DETAIL_DEMANGLING_MSVC_HPP_
  8. #include <boost/dll/detail/demangling/mangled_storage_base.hpp>
  9. #include <iterator>
  10. #include <algorithm>
  11. #include <boost/type_traits/is_const.hpp>
  12. #include <boost/type_traits/is_volatile.hpp>
  13. #include <boost/type_traits/is_lvalue_reference.hpp>
  14. #include <boost/type_traits/is_rvalue_reference.hpp>
  15. #include <boost/type_traits/function_traits.hpp>
  16. #include <boost/type_traits/remove_reference.hpp>
  17. #include <boost/spirit/home/x3.hpp>
  18. namespace boost { namespace dll { namespace detail {
  19. class mangled_storage_impl : public mangled_storage_base
  20. {
  21. template<typename T>
  22. struct dummy {};
  23. template<typename Return, typename ...Args>
  24. std::vector<std::string> get_func_params(dummy<Return(Args...)>) const
  25. {
  26. return {get_name<Args>()...};
  27. }
  28. template<typename Return, typename ...Args>
  29. std::string get_return_type(dummy<Return(Args...)>) const
  30. {
  31. return get_name<Return>();
  32. }
  33. //function to remove preceeding 'class ' or 'struct ' if the are given in this format.
  34. inline static void trim_typename(std::string & val);
  35. public:
  36. using ctor_sym = std::string;
  37. using dtor_sym = std::string;
  38. using mangled_storage_base::mangled_storage_base;
  39. template<typename T>
  40. std::string get_variable(const std::string &name) const;
  41. template<typename Func>
  42. std::string get_function(const std::string &name) const;
  43. template<typename Class, typename Func>
  44. std::string get_mem_fn(const std::string &name) const;
  45. template<typename Signature>
  46. ctor_sym get_constructor() const;
  47. template<typename Class>
  48. dtor_sym get_destructor() const;
  49. template<typename T> //overload, does not need to virtual.
  50. std::string get_name() const
  51. {
  52. auto nm = mangled_storage_base::get_name<T>();
  53. trim_typename(nm);
  54. return nm;
  55. }
  56. template<typename T>
  57. std::string get_vtable() const;
  58. template<typename T>
  59. std::vector<std::string> get_related() const;
  60. };
  61. void mangled_storage_impl::trim_typename(std::string & val)
  62. {
  63. //remove preceeding class or struct, because you might want to use a struct as class, et vice versa
  64. if (val.size() >= 6)
  65. {
  66. using namespace std;
  67. static constexpr char class_ [7] = "class ";
  68. static constexpr char struct_[8] = "struct ";
  69. if (equal(begin(class_), end(class_)-1, val.begin())) //aklright, starts with 'class '
  70. val.erase(0, 6);
  71. else if (val.size() >= 7)
  72. if (equal(begin(struct_), end(struct_)-1, val.begin()))
  73. val.erase(0, 7);
  74. }
  75. }
  76. namespace parser
  77. {
  78. namespace x3 = spirit::x3;
  79. auto ptr_rule_impl(std::integral_constant<std::size_t, 32>)
  80. {
  81. return -((-x3::space) >> "__ptr32");
  82. }
  83. auto ptr_rule_impl(std::integral_constant<std::size_t, 64>)
  84. {
  85. return -((-x3::space) >> "__ptr64");
  86. }
  87. auto ptr_rule() { return ptr_rule_impl(std::integral_constant<std::size_t, sizeof(std::size_t)*8>());}
  88. auto const visibility = ("public:" | x3::lit("protected:") | "private:");
  89. auto const virtual_ = x3::space >> "virtual";
  90. auto const static_ = x3::space >> x3::lit("static") ;
  91. auto const_rule_impl(true_type ) {return x3::space >> "const";};
  92. auto const_rule_impl(false_type) {return x3::eps;};
  93. template<typename T>
  94. auto const_rule() {using t = is_const<typename remove_reference<T>::type>; return const_rule_impl(t());}
  95. auto volatile_rule_impl(true_type ) {return x3::space >> "volatile";};
  96. auto volatile_rule_impl(false_type) {return x3::eps;};
  97. template<typename T>
  98. auto volatile_rule() {using t = is_volatile<typename remove_reference<T>::type>; return volatile_rule_impl(t());}
  99. auto inv_const_rule_impl(true_type ) {return "const" >> x3::space ;};
  100. auto inv_const_rule_impl(false_type) {return x3::eps;};
  101. template<typename T>
  102. auto inv_const_rule() {using t = is_const<typename remove_reference<T>::type>; return inv_const_rule_impl(t());}
  103. auto inv_volatile_rule_impl(true_type ) {return "volatile" >> x3::space;};
  104. auto inv_volatile_rule_impl(false_type) {return x3::eps;};
  105. template<typename T>
  106. auto inv_volatile_rule() {using t = is_volatile<typename remove_reference<T>::type>; return inv_volatile_rule_impl(t());}
  107. auto reference_rule_impl(false_type, false_type) {return x3::eps;}
  108. auto reference_rule_impl(true_type, false_type) {return x3::space >>"&" ;}
  109. auto reference_rule_impl(false_type, true_type ) {return x3::space >>"&&" ;}
  110. template<typename T>
  111. 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());}
  112. auto const class_ = ("class" | x3::lit("struct"));
  113. //it takes a string, because it may be overloaded.
  114. template<typename T>
  115. auto type_rule(const std::string & type_name)
  116. {
  117. using namespace std;
  118. return -(class_ >> x3::space)>> x3::string(type_name) >>
  119. const_rule<T>() >>
  120. volatile_rule<T>() >>
  121. reference_rule<T>() >>
  122. ptr_rule();
  123. }
  124. template<>
  125. auto type_rule<void>(const std::string &) { return x3::string("void"); };
  126. auto const cdecl_ = "__cdecl" >> x3::space;
  127. auto const stdcall = "__stdcall" >> x3::space;
  128. #if defined(_WIN64)//seems to be necessary by msvc 14-x64
  129. auto const thiscall = "__cdecl" >> x3::space;
  130. #else
  131. auto const thiscall = "__thiscall" >> x3::space;
  132. #endif
  133. template<typename Return, typename Arg>
  134. auto arg_list(const mangled_storage_impl & ms, Return (*)(Arg))
  135. {
  136. using namespace std;
  137. return type_rule<Arg>(ms.get_name<Arg>());
  138. }
  139. template<typename Return, typename First, typename Second, typename ...Args>
  140. auto arg_list(const mangled_storage_impl & ms, Return (*)(First, Second, Args...))
  141. {
  142. using next_type = Return (*)(Second, Args...);
  143. return type_rule<First>(ms.get_name<First>()) >> x3::char_(',') >> arg_list(ms, next_type());
  144. }
  145. template<typename Return>
  146. auto arg_list(const mangled_storage_impl& /*ms*/, Return (*)())
  147. {
  148. return x3::string("void");
  149. }
  150. }
  151. template<typename T> std::string mangled_storage_impl::get_variable(const std::string &name) const
  152. {
  153. using namespace std;
  154. using namespace boost;
  155. namespace x3 = spirit::x3;
  156. using namespace parser;
  157. auto type_name = get_name<T>();
  158. auto matcher =
  159. -(visibility >> static_ >> x3::space) >> //it may be a static class-member
  160. parser::type_rule<T>(type_name) >> x3::space >>
  161. name;
  162. auto predicate = [&](const mangled_storage_base::entry & e)
  163. {
  164. if (e.demangled == name)//maybe not mangled,
  165. return true;
  166. auto itr = e.demangled.begin();
  167. auto end = e.demangled.end();
  168. auto res = x3::parse(itr, end, matcher);
  169. return res && (itr == end);
  170. };
  171. auto found = std::find_if(storage_.begin(), storage_.end(), predicate);
  172. if (found != storage_.end())
  173. return found->mangled;
  174. else
  175. return "";
  176. }
  177. template<typename Func> std::string mangled_storage_impl::get_function(const std::string &name) const
  178. {
  179. namespace x3 = spirit::x3;
  180. using namespace parser;
  181. using func_type = Func*;
  182. using return_type = typename function_traits<Func>::result_type;
  183. std::string return_type_name = get_name<return_type>();
  184. auto matcher =
  185. -(visibility >> static_ >> x3::space) >> //it may be a static class-member, which does however not have the static attribute.
  186. parser::type_rule<return_type>(return_type_name) >> x3::space >>
  187. cdecl_ >> //cdecl declaration for methods. stdcall cannot be
  188. name >> x3::lit('(') >> parser::arg_list(*this, func_type()) >> x3::lit(')') >> parser::ptr_rule();
  189. auto predicate = [&](const mangled_storage_base::entry & e)
  190. {
  191. if (e.demangled == name)//maybe not mangled,
  192. return true;
  193. auto itr = e.demangled.begin();
  194. auto end = e.demangled.end();
  195. auto res = x3::parse(itr, end, matcher);
  196. return res && (itr == end);
  197. };
  198. auto found = std::find_if(storage_.begin(), storage_.end(), predicate);
  199. if (found != storage_.end())
  200. return found->mangled;
  201. else
  202. return "";
  203. }
  204. template<typename Class, typename Func>
  205. std::string mangled_storage_impl::get_mem_fn(const std::string &name) const
  206. {
  207. namespace x3 = spirit::x3;
  208. using namespace parser;
  209. using func_type = Func*;
  210. using return_type = typename function_traits<Func>::result_type;
  211. auto return_type_name = get_name<return_type>();
  212. auto cname = get_name<Class>();
  213. auto matcher =
  214. visibility >> -virtual_ >> x3::space >>
  215. parser::type_rule<return_type>(return_type_name) >> x3::space >>
  216. thiscall >> //cdecl declaration for methods. stdcall cannot be
  217. cname >> "::" >> name >>
  218. x3::lit('(') >> parser::arg_list(*this, func_type()) >> x3::lit(')') >>
  219. inv_const_rule<Class>() >> inv_volatile_rule<Class>() >> parser::ptr_rule();
  220. auto predicate = [&](const mangled_storage_base::entry & e)
  221. {
  222. auto itr = e.demangled.begin();
  223. auto end = e.demangled.end();
  224. auto res = x3::parse(itr, end, matcher);
  225. return res && (itr == end);
  226. };
  227. auto found = std::find_if(storage_.begin(), storage_.end(), predicate);
  228. if (found != storage_.end())
  229. return found->mangled;
  230. else
  231. return "";
  232. }
  233. template<typename Signature>
  234. auto mangled_storage_impl::get_constructor() const -> ctor_sym
  235. {
  236. namespace x3 = spirit::x3;
  237. using namespace parser;
  238. using func_type = Signature*;
  239. std::string ctor_name; // = class_name + "::" + name;
  240. std::string unscoped_cname; //the unscoped class-name
  241. {
  242. auto class_name = get_return_type(dummy<Signature>());
  243. auto pos = class_name.rfind("::");
  244. if (pos == std::string::npos)
  245. {
  246. ctor_name = class_name+ "::" + class_name ;
  247. unscoped_cname = class_name;
  248. }
  249. else
  250. {
  251. unscoped_cname = class_name.substr(pos+2) ;
  252. ctor_name = class_name+ "::" + unscoped_cname;
  253. }
  254. }
  255. auto matcher =
  256. visibility >> x3::space >>
  257. thiscall >> //cdecl declaration for methods. stdcall cannot be
  258. ctor_name >>
  259. x3::lit('(') >> parser::arg_list(*this, func_type()) >> x3::lit(')') >> parser::ptr_rule();
  260. auto predicate = [&](const mangled_storage_base::entry & e)
  261. {
  262. auto itr = e.demangled.begin();
  263. auto end = e.demangled.end();
  264. auto res = x3::parse(itr, end, matcher);
  265. return res && (itr == end);
  266. };
  267. auto f = std::find_if(storage_.begin(), storage_.end(), predicate);
  268. if (f != storage_.end())
  269. return f->mangled;
  270. else
  271. return "";
  272. }
  273. template<typename Class>
  274. auto mangled_storage_impl::get_destructor() const -> dtor_sym
  275. {
  276. namespace x3 = spirit::x3;
  277. using namespace parser;
  278. std::string dtor_name; // = class_name + "::" + name;
  279. std::string unscoped_cname; //the unscoped class-name
  280. {
  281. auto class_name = get_name<Class>();
  282. auto pos = class_name.rfind("::");
  283. if (pos == std::string::npos)
  284. {
  285. dtor_name = class_name+ "::~" + class_name + "(void)";
  286. unscoped_cname = class_name;
  287. }
  288. else
  289. {
  290. unscoped_cname = class_name.substr(pos+2) ;
  291. dtor_name = class_name+ "::~" + unscoped_cname + "(void)";
  292. }
  293. }
  294. auto matcher =
  295. visibility >> -virtual_ >> x3::space >>
  296. thiscall >> //cdecl declaration for methods. stdcall cannot be
  297. dtor_name >> parser::ptr_rule();
  298. auto predicate = [&](const mangled_storage_base::entry & e)
  299. {
  300. auto itr = e.demangled.begin();
  301. auto end = e.demangled.end();
  302. auto res = x3::parse(itr, end, matcher);
  303. return res && (itr == end);
  304. };
  305. auto found = std::find_if(storage_.begin(), storage_.end(), predicate);
  306. if (found != storage_.end())
  307. return found->mangled;
  308. else
  309. return "";
  310. }
  311. template<typename T>
  312. std::string mangled_storage_impl::get_vtable() const
  313. {
  314. std::string id = "const " + get_name<T>() + "::`vftable'";
  315. auto predicate = [&](const mangled_storage_base::entry & e)
  316. {
  317. return e.demangled == id;
  318. };
  319. auto found = std::find_if(storage_.begin(), storage_.end(), predicate);
  320. if (found != storage_.end())
  321. return found->mangled;
  322. else
  323. return "";
  324. }
  325. template<typename T>
  326. std::vector<std::string> mangled_storage_impl::get_related() const
  327. {
  328. std::vector<std::string> ret;
  329. auto name = get_name<T>();
  330. for (auto & c : storage_)
  331. {
  332. if (c.demangled.find(name) != std::string::npos)
  333. ret.push_back(c.demangled);
  334. }
  335. return ret;
  336. }
  337. }}}
  338. #endif /* BOOST_DLL_DETAIL_DEMANGLING_MSVC_HPP_ */