clang API Documentation

TargetCXXABI.h
Go to the documentation of this file.
00001 //===--- TargetCXXABI.h - C++ ABI Target Configuration ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief Defines the TargetCXXABI class, which abstracts details of the
00012 /// C++ ABI that we're targeting.
00013 ///
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CLANG_BASIC_TARGETCXXABI_H
00017 #define LLVM_CLANG_BASIC_TARGETCXXABI_H
00018 
00019 #include "llvm/ADT/Triple.h"
00020 #include "llvm/Support/ErrorHandling.h"
00021 
00022 namespace clang {
00023 
00024 /// \brief The basic abstraction for the target C++ ABI.
00025 class TargetCXXABI {
00026 public:
00027   /// \brief The basic C++ ABI kind.
00028   enum Kind {
00029     /// The generic Itanium ABI is the standard ABI of most open-source
00030     /// and Unix-like platforms.  It is the primary ABI targeted by
00031     /// many compilers, including Clang and GCC.
00032     ///
00033     /// It is documented here:
00034     ///   http://www.codesourcery.com/public/cxx-abi/
00035     GenericItanium,
00036 
00037     /// The generic ARM ABI is a modified version of the Itanium ABI
00038     /// proposed by ARM for use on ARM-based platforms.
00039     ///
00040     /// These changes include:
00041     ///   - the representation of member function pointers is adjusted
00042     ///     to not conflict with the 'thumb' bit of ARM function pointers;
00043     ///   - constructors and destructors return 'this';
00044     ///   - guard variables are smaller;
00045     ///   - inline functions are never key functions;
00046     ///   - array cookies have a slightly different layout;
00047     ///   - additional convenience functions are specified;
00048     ///   - and more!
00049     ///
00050     /// It is documented here:
00051     ///    http://infocenter.arm.com
00052     ///                    /help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
00053     GenericARM,
00054 
00055     /// The iOS ABI is a partial implementation of the ARM ABI.
00056     /// Several of the features of the ARM ABI were not fully implemented
00057     /// in the compilers that iOS was launched with.
00058     ///
00059     /// Essentially, the iOS ABI includes the ARM changes to:
00060     ///   - member function pointers,
00061     ///   - guard variables,
00062     ///   - array cookies, and
00063     ///   - constructor/destructor signatures.
00064     iOS,
00065 
00066     /// The iOS 64-bit ABI is follows ARM's published 64-bit ABI more
00067     /// closely, but we don't guarantee to follow it perfectly.
00068     ///
00069     /// It is documented here:
00070     ///    http://infocenter.arm.com
00071     ///                  /help/topic/com.arm.doc.ihi0059a/IHI0059A_cppabi64.pdf
00072     iOS64,
00073 
00074     /// The generic AArch64 ABI is also a modified version of the Itanium ABI,
00075     /// but it has fewer divergences than the 32-bit ARM ABI.
00076     ///
00077     /// The relevant changes from the generic ABI in this case are:
00078     ///   - representation of member function pointers adjusted as in ARM.
00079     ///   - guard variables  are smaller.
00080     GenericAArch64,
00081 
00082     /// The Microsoft ABI is the ABI used by Microsoft Visual Studio (and
00083     /// compatible compilers).
00084     ///
00085     /// FIXME: should this be split into Win32 and Win64 variants?
00086     ///
00087     /// Only scattered and incomplete official documentation exists.
00088     Microsoft
00089   };
00090 
00091 private:
00092   // Right now, this class is passed around as a cheap value type.
00093   // If you add more members, especially non-POD members, please
00094   // audit the users to pass it by reference instead.
00095   Kind TheKind;
00096 
00097 public:
00098   /// A bogus initialization of the platform ABI.
00099   TargetCXXABI() : TheKind(GenericItanium) {}
00100 
00101   TargetCXXABI(Kind kind) : TheKind(kind) {}
00102 
00103   void set(Kind kind) {
00104     TheKind = kind;
00105   }
00106 
00107   Kind getKind() const { return TheKind; }
00108 
00109   /// \brief Does this ABI generally fall into the Itanium family of ABIs?
00110   bool isItaniumFamily() const {
00111     switch (getKind()) {
00112     case GenericAArch64:
00113     case GenericItanium:
00114     case GenericARM:
00115     case iOS:
00116     case iOS64:
00117       return true;
00118 
00119     case Microsoft:
00120       return false;
00121     }
00122     llvm_unreachable("bad ABI kind");
00123   }
00124 
00125   /// \brief Is this ABI an MSVC-compatible ABI?
00126   bool isMicrosoft() const {
00127     switch (getKind()) {
00128     case GenericAArch64:
00129     case GenericItanium:
00130     case GenericARM:
00131     case iOS:
00132     case iOS64:
00133       return false;
00134 
00135     case Microsoft:
00136       return true;
00137     }
00138     llvm_unreachable("bad ABI kind");
00139   }
00140 
00141   /// \brief Is the default C++ member function calling convention
00142   /// the same as the default calling convention?
00143   bool isMemberFunctionCCDefault() const {
00144     // Right now, this is always false for Microsoft.
00145     return !isMicrosoft();
00146   }
00147 
00148   /// Are arguments to a call destroyed left to right in the callee?
00149   /// This is a fundamental language change, since it implies that objects
00150   /// passed by value do *not* live to the end of the full expression.
00151   /// Temporaries passed to a function taking a const reference live to the end
00152   /// of the full expression as usual.  Both the caller and the callee must
00153   /// have access to the destructor, while only the caller needs the
00154   /// destructor if this is false.
00155   bool areArgsDestroyedLeftToRightInCallee() const {
00156     return isMicrosoft();
00157   }
00158 
00159   /// \brief Does this ABI have different entrypoints for complete-object
00160   /// and base-subobject constructors?
00161   bool hasConstructorVariants() const {
00162     return isItaniumFamily();
00163   }
00164 
00165   /// \brief Does this ABI allow virtual bases to be primary base classes?
00166   bool hasPrimaryVBases() const {
00167     return isItaniumFamily();
00168   }
00169 
00170   /// \brief Does this ABI use key functions?  If so, class data such as the
00171   /// vtable is emitted with strong linkage by the TU containing the key
00172   /// function.
00173   bool hasKeyFunctions() const {
00174     return isItaniumFamily();
00175   }
00176 
00177   /// \brief Can an out-of-line inline function serve as a key function?
00178   ///
00179   /// This flag is only useful in ABIs where type data (for example,
00180   /// v-tables and type_info objects) are emitted only after processing
00181   /// the definition of a special "key" virtual function.  (This is safe
00182   /// because the ODR requires that every virtual function be defined
00183   /// somewhere in a program.)  This usually permits such data to be
00184   /// emitted in only a single object file, as opposed to redundantly
00185   /// in every object file that requires it.
00186   ///
00187   /// One simple and common definition of "key function" is the first
00188   /// virtual function in the class definition which is not defined there.
00189   /// This rule works very well when that function has a non-inline
00190   /// definition in some non-header file.  Unfortunately, when that
00191   /// function is defined inline, this rule requires the type data
00192   /// to be emitted weakly, as if there were no key function.
00193   ///
00194   /// The ARM ABI observes that the ODR provides an additional guarantee:
00195   /// a virtual function is always ODR-used, so if it is defined inline,
00196   /// that definition must appear in every translation unit that defines
00197   /// the class.  Therefore, there is no reason to allow such functions
00198   /// to serve as key functions.
00199   ///
00200   /// Because this changes the rules for emitting type data,
00201   /// it can cause type data to be emitted with both weak and strong
00202   /// linkage, which is not allowed on all platforms.  Therefore,
00203   /// exploiting this observation requires an ABI break and cannot be
00204   /// done on a generic Itanium platform.
00205   bool canKeyFunctionBeInline() const {
00206     switch (getKind()) {
00207     case GenericARM:
00208     case iOS64:
00209       return false;
00210 
00211     case GenericAArch64:
00212     case GenericItanium:
00213     case iOS:   // old iOS compilers did not follow this rule
00214     case Microsoft:
00215       return true;
00216     }
00217     llvm_unreachable("bad ABI kind");
00218   }
00219 
00220   /// When is record layout allowed to allocate objects in the tail
00221   /// padding of a base class?
00222   ///
00223   /// This decision cannot be changed without breaking platform ABI
00224   /// compatibility, and yet it is tied to language guarantees which
00225   /// the committee has so far seen fit to strengthen no less than
00226   /// three separate times:
00227   ///   - originally, there were no restrictions at all;
00228   ///   - C++98 declared that objects could not be allocated in the
00229   ///     tail padding of a POD type;
00230   ///   - C++03 extended the definition of POD to include classes
00231   ///     containing member pointers; and
00232   ///   - C++11 greatly broadened the definition of POD to include
00233   ///     all trivial standard-layout classes.
00234   /// Each of these changes technically took several existing
00235   /// platforms and made them permanently non-conformant.
00236   enum TailPaddingUseRules {
00237     /// The tail-padding of a base class is always theoretically
00238     /// available, even if it's POD.  This is not strictly conforming
00239     /// in any language mode.
00240     AlwaysUseTailPadding,
00241 
00242     /// Only allocate objects in the tail padding of a base class if
00243     /// the base class is not POD according to the rules of C++ TR1.
00244     /// This is non-strictly conforming in C++11 mode.
00245     UseTailPaddingUnlessPOD03,
00246 
00247     /// Only allocate objects in the tail padding of a base class if
00248     /// the base class is not POD according to the rules of C++11.
00249     UseTailPaddingUnlessPOD11
00250   };
00251   TailPaddingUseRules getTailPaddingUseRules() const {
00252     switch (getKind()) {
00253     // To preserve binary compatibility, the generic Itanium ABI has
00254     // permanently locked the definition of POD to the rules of C++ TR1,
00255     // and that trickles down to all the derived ABIs.
00256     case GenericItanium:
00257     case GenericAArch64:
00258     case GenericARM:
00259     case iOS:
00260       return UseTailPaddingUnlessPOD03;
00261 
00262     // iOS on ARM64 uses the C++11 POD rules.  It does not honor the
00263     // Itanium exception about classes with over-large bitfields.
00264     case iOS64:
00265       return UseTailPaddingUnlessPOD11;
00266 
00267     // MSVC always allocates fields in the tail-padding of a base class
00268     // subobject, even if they're POD.
00269     case Microsoft:
00270       return AlwaysUseTailPadding;
00271     }
00272     llvm_unreachable("bad ABI kind");
00273   }
00274 
00275   /// Try to parse an ABI name, returning false on error.
00276   bool tryParse(llvm::StringRef name);
00277 
00278   friend bool operator==(const TargetCXXABI &left, const TargetCXXABI &right) {
00279     return left.getKind() == right.getKind();
00280   }
00281 
00282   friend bool operator!=(const TargetCXXABI &left, const TargetCXXABI &right) {
00283     return !(left == right);
00284   }
00285 };
00286 
00287 }  // end namespace clang
00288 
00289 #endif