TargetCXXABI.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- TargetCXXABI.h - C++ ABI Target Configuration ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// Defines the TargetCXXABI class, which abstracts details of the
  16. /// C++ ABI that we're targeting.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CLANG_BASIC_TARGETCXXABI_H
  20. #define LLVM_CLANG_BASIC_TARGETCXXABI_H
  21. #include <map>
  22. #include "clang/Basic/LLVM.h"
  23. #include "llvm/ADT/StringMap.h"
  24. #include "llvm/ADT/Triple.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. namespace clang {
  27. /// The basic abstraction for the target C++ ABI.
  28. class TargetCXXABI {
  29. public:
  30. /// The basic C++ ABI kind.
  31. enum Kind {
  32. #define CXXABI(Name, Str) Name,
  33. #include "TargetCXXABI.def"
  34. };
  35. private:
  36. // Right now, this class is passed around as a cheap value type.
  37. // If you add more members, especially non-POD members, please
  38. // audit the users to pass it by reference instead.
  39. Kind TheKind;
  40. static const auto &getABIMap() {
  41. static llvm::StringMap<Kind> ABIMap = {
  42. #define CXXABI(Name, Str) {Str, Name},
  43. #include "TargetCXXABI.def"
  44. };
  45. return ABIMap;
  46. }
  47. static const auto &getSpellingMap() {
  48. static std::map<Kind, std::string> SpellingMap = {
  49. #define CXXABI(Name, Str) {Name, Str},
  50. #include "TargetCXXABI.def"
  51. };
  52. return SpellingMap;
  53. }
  54. public:
  55. static Kind getKind(StringRef Name) { return getABIMap().lookup(Name); }
  56. static const auto &getSpelling(Kind ABIKind) {
  57. return getSpellingMap().find(ABIKind)->second;
  58. }
  59. static bool isABI(StringRef Name) {
  60. return getABIMap().find(Name) != getABIMap().end();
  61. }
  62. // Return true if this target should use the relative vtables C++ ABI by
  63. // default.
  64. static bool usesRelativeVTables(const llvm::Triple &T) {
  65. return T.isOSFuchsia();
  66. }
  67. /// A bogus initialization of the platform ABI.
  68. TargetCXXABI() : TheKind(GenericItanium) {}
  69. TargetCXXABI(Kind kind) : TheKind(kind) {}
  70. void set(Kind kind) {
  71. TheKind = kind;
  72. }
  73. Kind getKind() const { return TheKind; }
  74. // Check that the kind provided by the fc++-abi flag is supported on this
  75. // target. Users who want to experiment using different ABIs on specific
  76. // platforms can change this freely, but this function should be conservative
  77. // enough such that not all ABIs are allowed on all platforms. For example, we
  78. // probably don't want to allow usage of an ARM ABI on an x86 architecture.
  79. static bool isSupportedCXXABI(const llvm::Triple &T, Kind Kind) {
  80. switch (Kind) {
  81. case GenericARM:
  82. return T.isARM() || T.isAArch64();
  83. case iOS:
  84. case WatchOS:
  85. case AppleARM64:
  86. return T.isOSDarwin();
  87. case Fuchsia:
  88. return T.isOSFuchsia();
  89. case GenericAArch64:
  90. return T.isAArch64();
  91. case GenericMIPS:
  92. return T.isMIPS();
  93. case WebAssembly:
  94. return T.isWasm();
  95. case XL:
  96. return T.isOSAIX();
  97. case GenericItanium:
  98. return true;
  99. case Microsoft:
  100. return T.isKnownWindowsMSVCEnvironment();
  101. }
  102. llvm_unreachable("invalid CXXABI kind");
  103. };
  104. /// Does this ABI generally fall into the Itanium family of ABIs?
  105. bool isItaniumFamily() const {
  106. switch (getKind()) {
  107. #define CXXABI(Name, Str)
  108. #define ITANIUM_CXXABI(Name, Str) case Name:
  109. #include "TargetCXXABI.def"
  110. return true;
  111. default:
  112. return false;
  113. }
  114. llvm_unreachable("bad ABI kind");
  115. }
  116. /// Is this ABI an MSVC-compatible ABI?
  117. bool isMicrosoft() const {
  118. switch (getKind()) {
  119. #define CXXABI(Name, Str)
  120. #define MICROSOFT_CXXABI(Name, Str) case Name:
  121. #include "TargetCXXABI.def"
  122. return true;
  123. default:
  124. return false;
  125. }
  126. llvm_unreachable("bad ABI kind");
  127. }
  128. /// Are member functions differently aligned?
  129. ///
  130. /// Many Itanium-style C++ ABIs require member functions to be aligned, so
  131. /// that a pointer to such a function is guaranteed to have a zero in the
  132. /// least significant bit, so that pointers to member functions can use that
  133. /// bit to distinguish between virtual and non-virtual functions. However,
  134. /// some Itanium-style C++ ABIs differentiate between virtual and non-virtual
  135. /// functions via other means, and consequently don't require that member
  136. /// functions be aligned.
  137. bool areMemberFunctionsAligned() const {
  138. switch (getKind()) {
  139. case WebAssembly:
  140. // WebAssembly doesn't require any special alignment for member functions.
  141. return false;
  142. case AppleARM64:
  143. case Fuchsia:
  144. case GenericARM:
  145. case GenericAArch64:
  146. case GenericMIPS:
  147. // TODO: ARM-style pointers to member functions put the discriminator in
  148. // the this adjustment, so they don't require functions to have any
  149. // special alignment and could therefore also return false.
  150. case GenericItanium:
  151. case iOS:
  152. case WatchOS:
  153. case Microsoft:
  154. case XL:
  155. return true;
  156. }
  157. llvm_unreachable("bad ABI kind");
  158. }
  159. /// Are arguments to a call destroyed left to right in the callee?
  160. /// This is a fundamental language change, since it implies that objects
  161. /// passed by value do *not* live to the end of the full expression.
  162. /// Temporaries passed to a function taking a const reference live to the end
  163. /// of the full expression as usual. Both the caller and the callee must
  164. /// have access to the destructor, while only the caller needs the
  165. /// destructor if this is false.
  166. bool areArgsDestroyedLeftToRightInCallee() const {
  167. return isMicrosoft();
  168. }
  169. /// Does this ABI have different entrypoints for complete-object
  170. /// and base-subobject constructors?
  171. bool hasConstructorVariants() const {
  172. return isItaniumFamily();
  173. }
  174. /// Does this ABI allow virtual bases to be primary base classes?
  175. bool hasPrimaryVBases() const {
  176. return isItaniumFamily();
  177. }
  178. /// Does this ABI use key functions? If so, class data such as the
  179. /// vtable is emitted with strong linkage by the TU containing the key
  180. /// function.
  181. bool hasKeyFunctions() const {
  182. return isItaniumFamily();
  183. }
  184. /// Can an out-of-line inline function serve as a key function?
  185. ///
  186. /// This flag is only useful in ABIs where type data (for example,
  187. /// vtables and type_info objects) are emitted only after processing
  188. /// the definition of a special "key" virtual function. (This is safe
  189. /// because the ODR requires that every virtual function be defined
  190. /// somewhere in a program.) This usually permits such data to be
  191. /// emitted in only a single object file, as opposed to redundantly
  192. /// in every object file that requires it.
  193. ///
  194. /// One simple and common definition of "key function" is the first
  195. /// virtual function in the class definition which is not defined there.
  196. /// This rule works very well when that function has a non-inline
  197. /// definition in some non-header file. Unfortunately, when that
  198. /// function is defined inline, this rule requires the type data
  199. /// to be emitted weakly, as if there were no key function.
  200. ///
  201. /// The ARM ABI observes that the ODR provides an additional guarantee:
  202. /// a virtual function is always ODR-used, so if it is defined inline,
  203. /// that definition must appear in every translation unit that defines
  204. /// the class. Therefore, there is no reason to allow such functions
  205. /// to serve as key functions.
  206. ///
  207. /// Because this changes the rules for emitting type data,
  208. /// it can cause type data to be emitted with both weak and strong
  209. /// linkage, which is not allowed on all platforms. Therefore,
  210. /// exploiting this observation requires an ABI break and cannot be
  211. /// done on a generic Itanium platform.
  212. bool canKeyFunctionBeInline() const {
  213. switch (getKind()) {
  214. case AppleARM64:
  215. case Fuchsia:
  216. case GenericARM:
  217. case WebAssembly:
  218. case WatchOS:
  219. return false;
  220. case GenericAArch64:
  221. case GenericItanium:
  222. case iOS: // old iOS compilers did not follow this rule
  223. case Microsoft:
  224. case GenericMIPS:
  225. case XL:
  226. return true;
  227. }
  228. llvm_unreachable("bad ABI kind");
  229. }
  230. /// When is record layout allowed to allocate objects in the tail
  231. /// padding of a base class?
  232. ///
  233. /// This decision cannot be changed without breaking platform ABI
  234. /// compatibility. In ISO C++98, tail padding reuse was only permitted for
  235. /// non-POD base classes, but that restriction was removed retroactively by
  236. /// DR 43, and tail padding reuse is always permitted in all de facto C++
  237. /// language modes. However, many platforms use a variant of the old C++98
  238. /// rule for compatibility.
  239. enum TailPaddingUseRules {
  240. /// The tail-padding of a base class is always theoretically
  241. /// available, even if it's POD.
  242. AlwaysUseTailPadding,
  243. /// Only allocate objects in the tail padding of a base class if
  244. /// the base class is not POD according to the rules of C++ TR1.
  245. UseTailPaddingUnlessPOD03,
  246. /// Only allocate objects in the tail padding of a base class if
  247. /// the base class is not POD according to the rules of C++11.
  248. UseTailPaddingUnlessPOD11
  249. };
  250. TailPaddingUseRules getTailPaddingUseRules() const {
  251. switch (getKind()) {
  252. // To preserve binary compatibility, the generic Itanium ABI has
  253. // permanently locked the definition of POD to the rules of C++ TR1,
  254. // and that trickles down to derived ABIs.
  255. case GenericItanium:
  256. case GenericAArch64:
  257. case GenericARM:
  258. case iOS:
  259. case GenericMIPS:
  260. case XL:
  261. return UseTailPaddingUnlessPOD03;
  262. // AppleARM64 and WebAssembly use the C++11 POD rules. They do not honor
  263. // the Itanium exception about classes with over-large bitfields.
  264. case AppleARM64:
  265. case Fuchsia:
  266. case WebAssembly:
  267. case WatchOS:
  268. return UseTailPaddingUnlessPOD11;
  269. // MSVC always allocates fields in the tail-padding of a base class
  270. // subobject, even if they're POD.
  271. case Microsoft:
  272. return AlwaysUseTailPadding;
  273. }
  274. llvm_unreachable("bad ABI kind");
  275. }
  276. friend bool operator==(const TargetCXXABI &left, const TargetCXXABI &right) {
  277. return left.getKind() == right.getKind();
  278. }
  279. friend bool operator!=(const TargetCXXABI &left, const TargetCXXABI &right) {
  280. return !(left == right);
  281. }
  282. };
  283. } // end namespace clang
  284. #endif
  285. #ifdef __GNUC__
  286. #pragma GCC diagnostic pop
  287. #endif